I am new to ML and tensorflow
I have a python script to load and train my model, which trains very fast, but being a newbie, I don’t really know what it should do.
The source images are vertically oriented pennies (lincoln’s head is upright) @ 400x400x3
my image dataset is built using only 10 master images that I augment manually to get rotated and shifted images in the dataset
10 images x 90 rotations == 900 images non-shifted
or
10 images x 90 rotations x 17 shifts == 15300 images shifted
I have not been able to find an example of building a custom image dataset like this. all of the examples seem to focus on using a pre-existing mnist dataset, or using image_dataset_from_directory, which expects all of the files to exist.
Because I an unsure if the problem is image related, I am only using the non-shifted images for testing.
The test images are what the training images were created from, before they were rotated to the correct orientation, so in my deductive reasoning, I should have a high probability of matching on something….
When I run predictions from test images, the results seem random.
I have tried various layers, image resolutions, all with similar results.
while training the accuracy seems very low to me.
again, I am new to ML and tensorflow, so I don’t know what “normal” is…
Because the issue (or issues) can be in so many places, I don’t really know where to start.
images:
def LoadImage(path):
# load and normalize an image
#print('loading image '+path)
img = cv2.imread(path)
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.astype(float) / 255.0
return img
baseimg = LoadImage(TrainingImagesDir+f)
for angle in range(0,360,4):
rotated = RotateImage(baseimg, angle)
label = 0 + int(angle/4)
train_images.append(rotated)
train_labels.append(label)
dataset:
train_images_tensor = tf.convert_to_tensor(train_images, name='train images')
train_labels_tensor = tf.convert_to_tensor(train_labels, name='train labels')
train_ds = tf.data.Dataset.from_tensors((train_images_tensor, train_labels_tensor))
model:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(IMAGE_SIZE,IMAGE_SIZE)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(len(arrClasses))
])
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
opt = tf.keras.optimizers.Adam(learning_rate=1e-5)
model.compile(
optimizer=opt,
loss=loss_fn,
metrics=['accuracy']
)
training:
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=LogDir)
model.fit(
train_ds,
validation_data=valid_ds,
verbose=1,
epochs=EPOCHS,
callbacks=[tensorboard_callback]
)
I have tried to change the layers by referencing different sample code online, but I do not understand how to pick the different layers to identify “features”. I haven’t found a good resource to guide me.
Before I go down the rabbit hole of fixing the model definition, I would like to confirm that is the issue.
Epoch 1/10 1/1 [==============================] - 1s 938ms/step - loss: 4.7652 - accuracy: 0.0116 - val_loss: 4.6423 - val_accuracy: 0.0000e+00 Epoch 2/10 1/1 [==============================] - 0s 313ms/step - loss: 4.7536 - accuracy: 0.0112 - val_loss: 4.6310 - val_accuracy: 0.0000e+00 Epoch 3/10 1/1 [==============================] - 0s 266ms/step - loss: 4.7422 - accuracy: 0.0109 - val_loss: 4.6199 - val_accuracy: 0.0000e+00 Epoch 4/10 1/1 [==============================] - 0s 266ms/step - loss: 4.7329 - accuracy: 0.0119 - val_loss: 4.6088 - val_accuracy: 0.0000e+00 Epoch 5/10 1/1 [==============================] - 0s 328ms/step - loss: 4.7249 - accuracy: 0.0125 - val_loss: 4.5982 - val_accuracy: 0.0000e+00 Epoch 6/10 1/1 [==============================] - 0s 423ms/step - loss: 4.7147 - accuracy: 0.0128 - val_loss: 4.5878 - val_accuracy: 0.0000e+00 Epoch 7/10 1/1 [==============================] - 0s 312ms/step - loss: 4.7023 - accuracy: 0.0122 - val_loss: 4.5784 - val_accuracy: 0.0000e+00 Epoch 8/10 1/1 [==============================] - 0s 312ms/step - loss: 4.6969 - accuracy: 0.0112 - val_loss: 4.5697 - val_accuracy: 0.0000e+00 Epoch 9/10 1/1 [==============================] - 0s 360ms/step - loss: 4.6897 - accuracy: 0.0121 - val_loss: 4.5618 - val_accuracy: 0.0000e+00 Epoch 10/10 1/1 [==============================] - 0s 390ms/step - loss: 4.6806 - accuracy: 0.0120 - val_loss: 4.5546 - val_accuracy: 0.0000e+00
skeeter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.