0
$\begingroup$

I tried to implement ResNet50 model for Regression prediction. Below is my trial code. This code runs without error. You can also try it without dependency. how to improve so that the model is able to predict each data point?

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import os
import json
from math import*
import tensorflow as tf
from tensorflow.keras.optimizers import Adam, Adadelta
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Flatten, Dense, Dropout, Activation
from tensorflow.keras.applications.resnet import ResNet50
from tensorflow.keras.applications.imagenet_utils import preprocess_input
from keras.utils.np_utils import to_categorical
from tensorflow.keras.layers import Input
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import load_model
from keras.utils.np_utils import to_categorical
from sklearn.preprocessing import MinMaxScaler

np.seterr(divide='ignore', invalid='ignore')

Insole = pd.read_csv('1119_Rwalk40s1_list.txt', header=None, low_memory=False)
SIData =  np.asarray(Insole)
df = pd.read_csv('1119_Rwalk40s1.csv', low_memory=False)
columns = ['Fz', 'Mx', 'My']
selected_df = df[columns]
FCDatas = selected_df[:2050]
## End Load Data
SmartInsole = np.array(SIData[:2050])
FCData = np.array(FCDatas)
FCData = abs(FCData)

I am working with Keras and trying to fit a resnet50 to the data just to evaluate it. Below is the my resnet model structure:

Below is identity blok:

def identity_block(input_tensor,units):
"""The identity block is the block that has no conv layer at shortcut.
# Arguments
    input_tensor: input tensor
    units:output shape
# Returns
    Output tensor for the block.
"""
x = layers.Dense(units)(input_tensor)
x = layers.Activation('relu')(x)

x = layers.Dense(units)(x)
x = layers.Activation('relu')(x)

x = layers.Dense(units)(x)

x = layers.add([x, input_tensor])
x = layers.Activation('relu')(x)

return x

Below is dens_block:

def dens_block(input_tensor,units):
"""A block that has a dense layer at shortcut.
# Arguments
    input_tensor: input tensor
    unit: output tensor shape
# Returns
    Output tensor for the block.
"""
x = layers.Dense(units)(input_tensor)
x = layers.Activation('relu')(x)

x = layers.Dense(units)(x)
x = layers.Activation('relu')(x)

x = layers.Dense(units)(x)

shortcut = layers.Dense(units)(input_tensor)

x = layers.add([x, shortcut])
x = layers.Activation('relu')(x)
return x

Resnet50 model:

def ResNet50Regression():
Res_input = layers.Input(shape=(178,))
width = 128

x = dens_block(Res_input,width)
x = identity_block(x,width)
x = identity_block(x,width)

x = dens_block(x,width)
x = identity_block(x,width)
x = identity_block(x,width)

x = dens_block(x,width)
x = identity_block(x,width)
x = identity_block(x,width)

x = layers.Dense(1,activation="sigmoid")(x)
model = models.Model(inputs=Res_input, outputs=x)

return model

Model Fit

model = ResNet50Regression()
model.compile(loss='mse', optimizer=Adam(lr = 0.001), metrics=['mse'])
model.summary()
history = model.fit(X_train, y_train, batch_size=16, epochs=50, validation_data=(X_test, y_test), verbose=2)
ypred = model.predict(xscale)
x=[]
colors=['red','green','brown','teal','gray','black','maroon','orange','purple']
colors2=['green','red','orange','black','maroon','teal','blue','gray','brown']
# just update the x with correct scaling; use numpy array for faster computations
x = np.arange(0,2050)*60/2050
for i in range(0,3):
plt.figure(figsize=(15,6))
# plt.figure()
plt.plot(x,yscale[0:2050,i], 'rx', color=colors[i])
plt.plot(x,ypred[0:2050,i],'gx',markerfacecolor='none',color=colors2[i])
plt.title('Resnet50 Regression (Training Data)')
plt.ylabel('Force/Fz (N)')
plt.xlabel('Time(s)')
plt.legend(['Real value', 'Predicted Value'], loc='lower left')
plt.savefig('Regression Result.png'[i])
plt.show()

Prediction Result

$\endgroup$
3
  • $\begingroup$ Can you double-check you've implemented Resnet50 correctly. It's a convolutional model, but I can only see dense layers (and activations) in the code you have provided. $\endgroup$
    – Lynn
    Commented Nov 29, 2022 at 10:46
  • $\begingroup$ so I still need convolution process in my model? but my data is only the data poin not the image $\endgroup$ Commented Nov 29, 2022 at 10:55
  • $\begingroup$ You probably don't want a convolutional model if you are not using images or sequences. But it confuses people when you say you are trying to implement resnet50 but then code up a non-convolutional model. $\endgroup$
    – Lynn
    Commented Nov 29, 2022 at 11:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.