I’m new on neural networks and i’m working on prediction for images. I would like to predict 101 data point from an image. I’ve tried the following code, but when I predict I get always the same prediction.
I’ve also tried with more epochs, different layers, different activation functions and got the same problem.
Any idea to solve this issue ?
Here is my code :
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image
import scipy.io
import os
import random
Trainingfolder =r '---\Dataset\Dataset1\Trainingset'
Testfolder = r'---\Dataset\Dataset1\Testset'
image_files = [os.path.join(Trainingfolder, f) for f in os.listdir(Trainingfolder) if f.endswith('.jpeg')]
plt.figure()
for i in range(5):
imgfile = random.choice(image_files)
img = image.imread(imgfile)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
ax=plt.subplot(2,5,i+1)
plt.imshow(img)
ax=plt.subplot(2,5,i+1+5)
plt.plot(output)
def create_dataset(datafolder):
image_files = [os.path.join(datafolder, f) for f in os.listdir(datafolder) if f.endswith('.jpeg')]
img_data_array = []
output_data = []
for imgfile in image_files:
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
outname = imgfile[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
output_data.append(output)
return img_data_array, output_data
img_Trainingdata, output_Trainingdata = create_dataset(Trainingfolder)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=(101,9,3)))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2), padding='same'))
model.add(tf.keras.layers.Conv2D(128, kernel_size=(3,3), activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2, 2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(3000, activation = 'relu'))
model.add(tf.keras.layers.Dense(101))
adam = tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False);
model.compile(optimizer=adam, loss='mse', metrics=["mse"])
model.summary()
Layer (type) | Output Shape | Param # |
---|---|---|
conv2d_2 (Conv2D) | (None, 99, 7, 64) | 1792 |
max_pooling2d (MaxPooling2D) | (None, 50, 4, 64) | 0 |
conv2d_3 (Conv2D) | (None, 48, 2, 128) | 73856 |
max_pooling2d_1 (MaxPooling2D) | (None, 24, 1, 128) | 0 |
flatten_1 (Flatten) | (None, 3072) | 0 |
dense_2 (Dense) | (None, 3000) | 9219000 |
dense_3 (Dense) | (None, 101) | 303101 |
Total params: 9,597,749
Trainable params: 9,597,749
Non-trainable params: 0
history = model.fit(x=np.array(img_Trainingdata, np.float64), y=np.array(output_Trainingdata, np.float64), epochs=1)
36/36 [==============================] - 2s 55ms/step - loss: 0.0639 - mse: 0.0639
def load_Testdata(dataname):
img_data_array = []
imag = image.imread(imgfile)
imag = np.array(imag)
imag = imag.astype('float64')
imag /= 255
img_data_array.append(imag)
return img_data_array
image_test = [os.path.join(Testfolder, f) for f in os.listdir(Testfolder) if f.endswith('.jpeg')]
plt.figure()
for itest in image_test:
outname = itest[0:-5] + ".mat"
outfile = scipy.io.loadmat(outname, squeeze_me=True,struct_as_record=False)
output = outfile['output'].AnkleFlexExt
plt.plot(output, color='blue', label='Measured')
for itest in image_test:
img_test = load_Testdata(itest)
pred = model.predict (x=np.array(img_test, np.float64))
pred = pred[0]
plt.plot(pred, color='red', label='Predicted')
plt.title('Ankle Flex-Ext Moment')
plt.ylabel('Nm.kg$^{-1}$')
plt.xlabel('% cycle')
plt.show()