I’m trying to train a neural network to find the brightest pixel for each image in a dataset. I’ve started with a simple rgb images dataset with 3 random grey values:
def generate():
a = random.randrange(0, 255)
b = random.randrange(0, 255)
c = random.randrange(0, 255)
return a, b, c
def colors (a, b, c):
for m in range(height):
for n in range(30):
img.putpixel( (n,m), (a, a, a))
for m in range(height):
for n in range(30,60):
img.putpixel( (n,m), (b, b, b))
for m in range(height):
for n in range(60, 90):
img.putpixel( (n,m), (c, c, c))
return img
for i in range (1000):
a, b, c = generate()
width = 90
height = 90
img = Image.new('RGB', [width,height])
img = colors(a,b,c)
img.save('img/%d.bmp'%(i))
Then I create the array with the images and the label list with the highest pixel value:
import cv2
import glob
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
X = []
y = []
files = glob.glob ('img/*bmp')
for myFile in files:
image = cv2.imread (myFile)
biggest = np.amax(image)
y.append (biggest)
X.append (image)
X = np.array(X)
X = X.astype('float32') / 255
print(X.shape)
I set the train/test sizes and transform the label list into an array:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
y_train = np.asarray(y_train)
y_test = np.asarray(y_test)
And here comes trouble: I can’t even understand what kind of modeling problem is, because it looks different from classification problems. Here I have a different target value for each image and if I try to adapt some models I’ve already used (adjusting the values) I got very high loss and very low accuracy. Here a model:
from tensorflow.keras import datasets, layers, models
model = models.Sequential()
model.add(layers.Conv2D(32, (11, 11), activation='relu', input_shape=(90, 90, 3)))
model.add(layers.MaxPooling2D(5,5))
model.add(layers.Conv2D(64, (9, 9), activation='relu',))
model.add(layers.MaxPooling2D(4,4))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(255, activation = 'softmax'))
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
model.fit(X_train, y_train,
epochs=40,
shuffle=True,
validation_data=(X_test, y_test))
I also think I need to customize the loss function and the accuracy, because it’s not only a true/false problem: an output value close to the target can’t be considered as “false”. Now I’m not totally sure if the biggest problem is the training model or the loss function.