Home > Back-end >  "Cats vs Dogs" artificial neural network Tensorflow model.fit problems
"Cats vs Dogs" artificial neural network Tensorflow model.fit problems

Time:11-25

I am trying to learn the machine to recognize a cat or a dog when the algorithm is shown a picture of either a cat or dog. I am using Spyder as IDE, and Tensorflow.

We are supposed to run this code to train the AI:

#%%
from tensorflow.keras import layers
from tensorflow.keras import models

model = models.Sequential()

#%%
# 32 filters with 3x3 pixel kernels and ReLU activation for 150x150 RGB (= 3-channel) images
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))

# Downsample by picking max input from every 2x2 window of previous neurons
model.add(layers.MaxPooling2D((2, 2))) 

# Next layer of filters
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# More downsampling (important to reduce network size/complexity)
model.add(layers.MaxPooling2D((2, 2))) 
model.add(layers.Conv2D(128, (3, 3), activation='relu')) 
model.add(layers.MaxPooling2D((2, 2))) 
model.add(layers.Conv2D(128, (3, 3), activation='relu')) 
model.add(layers.MaxPooling2D((2, 2)))

#%%
model.add(layers.Flatten()) 
model.add(layers.Dense(512, activation='relu')) 
model.add(layers.Dense(1, activation='sigmoid'))

#%%
from tensorflow.keras import optimizers
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=1e-4),
              metrics=['acc'])

#%%
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_dir = "C:\\Users\\Nicholai\\Desktop\\dogs-vs-cats\small\\train"
validation_dir = "C:\\Users\\Nicholai\\Desktop\\dogs-vs-cats\\small\\val"

im_per_batch = 20 
im_size = (150,150) # Same as number of inputs in the network

train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=im_size,
    batch_size=im_per_batch,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_dir,
    target_size=im_size,
    batch_size=im_per_batch,
    class_mode='binary')

#%%
num_epochs = 30
history = model.fit_generator(
    train_generator,
    steps_per_epoch = train_generator.n // im_per_batch,
    epochs=num_epochs,
    validation_data = validation_generator,
    validation_steps = validation_generator.n // im_per_batch)

#%%
model.save('dogs-vs-cats-1.h5')
from keras.models import load_model
model = load_model('dogs-vs-cats-1.h5')

I get this error:

C:\Users\Nicholai\.spyder-py3\temp.py:61: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  history = model.fit_generator(
Traceback (most recent call last):

  File "C:\Users\Nicholai\.spyder-py3\temp.py", line 61, in <module>
    history = model.fit_generator(

  File "C:\Users\Nicholai\anaconda3\envs\CNN\lib\site-packages\keras\engine\training.py", line 2016, in fit_generator
    return self.fit(

  File "C:\Users\Nicholai\anaconda3\envs\CNN\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "C:\Users\Nicholai\anaconda3\envs\CNN\lib\site-packages\keras_preprocessing\image\affine_transformations.py", line 281, in apply_affine_transform
    raise ImportError('Image transformations require SciPy. '

ImportError: Image transformations require SciPy. Install SciPy.

I understand that Tensorflow newer than 2.0 can use model.fit() instead of model.fit_generator(). SciPy is installed When I try that I still get errors like this:

    Traceback (most recent call last):

  File "C:\Users\Nicholai\.spyder-py3\temp.py", line 61, in <module>
    history = model.fit(

  File "C:\Users\Nicholai\anaconda3\envs\CNN\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "C:\Users\Nicholai\anaconda3\envs\CNN\lib\site-packages\keras_preprocessing\image\affine_transformations.py", line 281, in apply_affine_transform
    raise ImportError('Image transformations require SciPy. '

What am I doing wrong?

CodePudding user response:

You even have an error written:

   ImportError('Image transformations require SciPy.')

and:

ImportError: Image transformations require SciPy. Install SciPy.

Just install the missing packages via pip install or whatever you use there for that ...

  • Related