I’m a begginer on tensorflow and I made a convolution model with tensorflow(sub-classing) to classify 10 differents draws. For that I used the quick draw dataset with images of size (1, 28, 28, 1).To compile the model I used :
loss : SparseCategoricalCrossentropy
optimizer : SGD
accuracy : SparseCategoricalAccuracy
the model :
@tf.keras.saving.register_keras_serializable()
class ConvModel(tf.keras.Model):
def __init__(self):
super(ConvModel, self).__init__()
self.conv1 = tf.keras.layers.Conv2D(32, 4, activation = 'relu', name = 'conv1', input_shape = (28, 28, 1))
self.conv2 = tf.keras.layers.Conv2D(32, 3, activation = 'relu', name = 'conv2')
self.conv3 = tf.keras.layers.Conv2D(32, 3, activation = 'relu', name = 'conv3')
self.flatten = tf.keras.layers.Flatten(name = 'flatten')
self.d1 = tf.keras.layers.Dense(32, activation = 'relu', name = 'd1')
self.d2 = tf.keras.layers.Dense(16, activation = 'relu', name = 'd2')
self.out = tf.keras.layers.Dense(10, activation = 'softmax', name = 'out')
def call(self, image):
conv1 = self.conv1(image)
conv2 = self.conv2(conv1)
conv3 = self.conv3(conv2)
flatten = self.flatten(conv3)
d1 = self.d1(flatten)
d2 = self.d2(d1)
output = self.out(d2)
return output
My model works and I tried to test him with my own drawings.But when I convert the png to numpy array I have not good dimensions to do a predict and I get an error because of convolution(I used imread function from matplotlib.image and from cv2).
Here the code I used to load datas and to normalize datas.
to load datas :
files = [name for name in os.listdir() if ".npy" in name]
max_size_per_cl = 1500
draw_class = []
# Evaluate the size of the dataset
size = 0
for name in files:
draws = np.load(name)
draws = draws[:max_size_per_cl]
size += draws.shape[0]
images = np.zeros((size, 28, 28))
targets = np.zeros((size,))
it = 0
t = 0
for name in files:
# Open each dataset and add the new class
draw_class.append(name.replace("full_numpy_bitmap_", "").replace(".npy", ""))
draws = np.load(name)
draws = draws[:max_size_per_cl] # Take only 10 000 draw
# Add images to the buffer
images[it:it+draws.shape[0]] = np.invert(draws.reshape(-1, 28, 28))
targets[it:it+draws.shape[0]] = t
# Iter
it += draws.shape[0]
t += 1
images = images.astype(np.float32)
# Shuffle dataset
indexes = np.arange(size)
np.random.shuffle(indexes)
images = images[indexes]
targets = targets[indexes]
images, images_valid, targets, targets_valid = train_test_split(images, targets, test_size=0.33)
to normalize datas :
scaler = StandardScaler()
scaled_images = scaler.fit_transform(images.reshape(-1, 28 * 28))
scaled_images_valid = scaler.transform(images_valid.reshape(-1, 28 * 28))
images = scaled_images.reshape(-1, 28, 28, 1)
images_valid = scaled_images_valid.reshape(-1, 28, 28, 1)
To solve the problem, I used np.resize and cv2.resize function.
but either it doesn’t work and I get an error from the convolution or it works but the prediction are really not good and the 10 softmax activations are very similar. it look like that :
array([[0.10894018, 0.09810086, 0.10048549, 0.08485338, 0.10409403,
0.09969998, 0.10958809, 0.09787109, 0.10079118, 0.0955757 ]],
dtype=float32)
Please help me.
Random693 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.