Cats_Vs_Dogs going nowhere

I can’t seem to get to 63% accuracy on my model no matter how I tweak the metaparameters.
Also, I get an error calling plotImages() that I can’t figure out how to fix. Anybody out there give me a clue here?

model = Sequential()

model.add(tf.keras.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3)))

#model.add(tf.keras.layers.Conv2D(128, (3,3), strides = (2,2), activation='relu'))

model.add(tf.keras.layers.Conv2D(64, (2,2), strides = (2,2), activation='relu'))

model.add(tf.keras.layers.MaxPooling2D(4,(2,2)))

model.add(tf.keras.layers.Conv2D(32, (2,2), strides = (2,2), activation='relu'))

model.add(tf.keras.layers.MaxPooling2D(8,(2,2)))

model.add(tf.keras.layers.Conv2D(16, (2,2), strides = (2,2), activation='relu'))

model.add(tf.keras.layers.MaxPooling2D(2,2))

model.add(Flatten())

model.add(tf.keras.layers.Dense(2, activation='relu'))

model.compile(optimizer='adam',

              loss='sparse_categorical_crossentropy',

              metrics=['accuracy'])

model.summary()


probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
#print(type(probability_model))
predictions = probability_model.predict(test_data_gen)#predictions is numpy.ndarray
probabilities = []#type=array
for p in predictions:
  p =(np.round(p))
  probabilities.append((p))
#print(predictions)
#print(probabilities[0])
get_one = []
for a in probabilities:
  #print(a[0])
  if a[0] > 0.5:
    get_one.append(1)
  else:
    get_one.append(0)
print(get_one)
#print(test_data_gen)

plotImages(test_data_gen, get_one)





[0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1]
/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py:706: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  x = np.array(x, subok=True, copy=copy)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-8876e0f18596> in <module>()
     18 #print(test_data_gen)
     19 
---> 20 plotImages(test_data_gen, get_one)

6 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py in safe_masked_invalid(x, copy)
    704 
    705 def safe_masked_invalid(x, copy=False):
--> 706     x = np.array(x, subok=True, copy=copy)
    707     if not x.dtype.isnative:
    708         # Note that the argument to `byteswap` is 'inplace',

ValueError: could not broadcast input array from shape (150,150,3) into shape (1,)

One major issue I had for a while was, the feature-map created internally was WAY to small.
Please think again about your layers. Conv2D with a (2,2) filter without padding reduces the size of the image by 1px in every dimension for example.
Similarly MaxPooling2D also reduces the size.
So while you try to filter out important features, you might very well delete a lot of information.

Though looking at my solution, I also included more dense layers.
And I would recommend using the shorthand for the layers like so

model = Sequential([
                  Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
                  MaxPooling2D((2, 2)),
                  Conv2D(64, (3, 3), activation='relu'),
                  MaxPooling2D((2,2)),
                  Flatten(),
                  Dense(16)
                  Dense(2)
])

This way you can more easily see it’s setup AND comment something out.

As for the plotImages… Well you gotta look at the 6 frames, the Traceback skipped ^^°

Somebody might help with the error if you post a shareable link to your notebook on colab, but this project takes a lot of time to run so you’re far more likely to get help on the error than on the model.

On the error, since the function has a default for the second parameter, try calling it with just the first parameter to see if the error persists.

On the model, I agree that you are probably losing too much data at each layer to succeed. My best one actually grew in size as the number of layers increased; I had modeled it on one I reference as “SVHN” model but I don’t remember the source. I was researching image classification networks and found it online. My suggestion is to start with a mix of convolutions, dense, dropout, and pooling layers and select one optimizer and loss function and a constant number of epochs and manually iterate. When the percent increases, move more in that direction and if it decreases try something different. If you get close, then you may be able to increase the number of epochs or adjust the dropouts to get to the target percent. It really is just trial and error; there is no magic involved.

By the way, there is a lazy approach to finish the task - the course goes over how to use pre-trained models and actually references one that is suitable for this task.
I tried using it after I trial-and-error’ed my way to my own working model.

following code displays thin ribbons of color for each picture. The shape is 150, 1How do I change that to display the full 150X150 picture?

count = 10
while count > 0:

  pics.append(next(test_data_gen)) # pics needs to be an array of pictures from test_data_gen
  count = count - 1

print(type(pics))
print(pics[5])
print(len(pics))
images = []
print(type(images))
for pic in pics:
  image = np.asarray(pic[0][0][0])
  plt.imshow(image)
  images.append(image)
#image2 = np.asarray(pics[0][0][0])
#image3 = np.asarray(pics[0][0][0])
plt.imshow(image)
#plt.imshow(image2)
#plt.imshow(image3)
plotImages(images)```

So… I tried the pre-trained model from the tf tutorial, and it wanted the photos to be size 224x224 instead of 150x150, so I changed that, and there is no improvement in my model’s accuracy. I am not finding any help on how to work with images in this particular situation. I think I’m gonna bail on this because I don’t have the math.

Hm… well I mean, the example model I posted was pretty close to my passing solution.
As for the pre-trained model, kinda hard to say where the issue might be with your code. But I don’t recall doing a lot to integrate it…
Anysway, math shouldn’t be the issue. Just experimenting with different layers and making sure not to add to many layers should help a lot… I certainly didn’t do any math and just tried until something worked ^^°

I’m getting the same loss 0.6931 - accuracy 0.5000 for almost every time I fit the model, and accuracy never got over 58%. So I’m going to start over from scratch because I feel like I’m in some kind of self-perpetuating loop.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.