<jupyter_start><jupyter_text>data import<jupyter_code>import pathlib
data_dir = pathlib.Path("/Users/j/Desktop/programming/ML/github/image_recognition/dogs vs cats datasets/Data/train")
print(data_dir)

image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)

CLASS_NAMES = np.array([item.name for item in data_dir.glob('*') if item.name != ".DS_Store"])
print(CLASS_NAMES)


# The 1./255 is to convert from uint8 to float32 in range [0,1]. Better for d\feeding to a neural net
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)



BATCH_SIZE = 32
#  Batch Sixe is how many images are we feeding in on one batch. It can be set to none. It is important to set it
# to optimise computing power
IMG_HEIGHT = 50
IMG_WIDTH = 50
STEPS_PER_EPOCH = np.ceil(image_count/BATCH_SIZE)

train_data_gen = image_generator.flow_from_directory(directory=str(data_dir),
                                                     batch_size=BATCH_SIZE,
                                                     shuffle=True,
                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                     class_mode='binary')
#  here class_mode='binary' is imp because it creates an output of [0,1] rather than [dog,cat]
# Mode for yielding the targets:
#     - `"binary"`: 1D numpy array of binary labels,
#     - `"categorical"`: 2D numpy array of one-hot encoded labels.
#         Supports multi-label output.
#     - `"input"`: images identical to input images (mainly used to
#         work with autoencoders),
#     - `"multi_output"`: list with the values of the different columns,
#     - `"raw"`: numpy array of values in `y_col` column(s),
#     - `"sparse"`: 1D numpy array of integer labels,
#     - `None`, no targets are returned (the generator will only yield
#         batches of image data, which is useful to use in
#         `model.predict_generator()`).<jupyter_output><empty_output><jupyter_text>creating the model<jupyter_code>model = Sequential([
    Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),
    MaxPooling2D(),
    Conv2D(32, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(64, 3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.summary()
total_train = image_count
batch_size = 128
epochs = 15
# history = model.fit_generator(
#     train_data_gen,
#     steps_per_epoch=total_train // batch_size,
#     epochs=epochs,
#     validation_data=val_data_gen,
#     validation_steps=total_val // batch_size
# )
history = model.fit_generator(
    train_data_gen,
    steps_per_epoch=total_train // batch_size,
    epochs=epochs,
)<jupyter_output>Epoch 1/15
195/195 [==============================] - 30s 156ms/step - loss: 0.6720 - accuracy: 0.5796
Epoch 2/15
195/195 [==============================] - 36s 184ms/step - loss: 0.6123 - accuracy: 0.6667
Epoch 3/15
195/195 [==============================] - 32s 167ms/step - loss: 0.5526 - accuracy: 0.7197
Epoch 4/15
195/195 [==============================] - 35s 180ms/step - loss: 0.5363 - accuracy: 0.7321
Epoch 5/15
195/195 [==============================] - 30s 153ms/step - loss: 0.5118 - accuracy: 0.7482
Epoch 6/15
195/195 [==============================] - 35s 181ms/step - loss: 0.4799 - accuracy: 0.7721
Epoch 7/15
195/195 [==============================] - 32s 166ms/step - loss: 0.4790 - accuracy: 0.7764
Epoch 8/15
195/195 [==============================] - 36s 184ms/step - loss: 0.4733 - accuracy: 0.7732
Epoch 9/15
195/195 [==============================] - 29s 147ms/step - loss: 0.4366 - accuracy: 0.7944
Epoch 10/15
195/195 [==============================] - 32s 163ms/step - l[...]<jupyter_text>Visualize training results<jupyter_code>import matplotlib.pyplot as plt
acc = history.history['accuracy']
# val_acc = history.history['val_accuracy']

loss = history.history['loss']
# val_loss = history.history['val_loss']

epochs_range = range(epochs)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
# plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
# plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18569903948772679, 'fraction_numerical': 0.06147278548559232, 'mean_word_length': 3.658051689860835, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '53276', 'license': 'MIT', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1519, 'n_words': 371, 'path': 'dogs vs cats datasets/Tensorflow_edited.ipynb', 'repo_name': 'xo28122000/image_recognition', 'size': '4685'}