<jupyter_start><jupyter_text>Mounting Google Drive and retrieving training set images<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
base_path = '/content/drive/My Drive/Colab Notebooks/mask-detector/'
data_dir_collected = pathlib.Path(os.path.join(base_path, 'data/training'))
data_dir_test = pathlib.Path(os.path.join(base_path, 'data/test'))
image_count_collected = len(list(data_dir_collected.glob('**/*.jpg')))
test_images_count = len(list(data_dir_test.glob('**/*.jpg')))
print('images collected', image_count_collected)
print('test images', test_images_count)
CLASS_NAMES = np.array([item.name for item in data_dir_collected.glob('*')])
print(CLASS_NAMES)
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
IMG_WIDTH = 160
IMG_HEIGHT = 160
IMG_SIZE = (IMG_WIDTH, IMG_HEIGHT)
train_data_gen = image_generator.flow_from_directory(directory=str(data_dir_collected),
                                                     shuffle=True,
                                                     target_size=IMG_SIZE,
                                                     classes = list(CLASS_NAMES))
def show_batch(image_batch, label_batch):
    plt.figure(figsize=(10,10))
    for n in range(25):
        ax = plt.subplot(5,5,n+1)
        plt.imshow(image_batch[n])
        plt.title(CLASS_NAMES[label_batch[n]==1][0].title())
        plt.axis('off')

image_batch, label_batch = next(train_data_gen)
show_batch(image_batch, label_batch)
list_ds_collected = tf.data.Dataset.list_files(str(data_dir_collected/'*/*'))
list_ds_test = tf.data.Dataset.list_files(str(data_dir_test/'*/*'))
def get_label(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  # The second to last is the class-directory
  return parts[-2] == CLASS_NAMES

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # Use `convert_image_dtype` to convert to floats in the [0,1] range.
  img = tf.image.convert_image_dtype(img, tf.float32)
  # resize the image to the desired size.
  return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])

def process_path(file_path):
  label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label
train_ds = list_ds_collected.map(process_path)
validation_ds = list_ds_collected.map(process_path)

for image, label in train_ds.take(1):
  print("Image shape: ", image.numpy().shape)
  print("Label: ", label.numpy())
def get_data_generator():
    return ImageDataGenerator(
        rotation_range=20,
        zoom_range=0.10,
        width_shift_range=0.1,
        height_shift_range=0.1,
        shear_range=0.15,
        horizontal_flip=True,
        fill_mode="nearest")
train_ds = train_ds.batch(image_count_collected)
validation_ds = validation_ds.batch(test_images_count)
datagen = get_data_generator()
image_batch, label_batch = next(iter(train_ds))
datagen.fit(image_batch)
IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')
base_model.trainable = False

feature_batch = base_model(image_batch)
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
prediction_layer = tf.keras.layers.Dense(len(CLASS_NAMES), activation='softmax')
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape)
reps = 50
training_ds = train_ds.repeat(reps)
X_training, Y_training = next(iter(training_ds))

for X, Y in training_ds:
    X_training = tf.concat([X, X_training], axis=0)
    Y_training = tf.concat([Y, Y_training], axis=0)

X_test, Y_test = next(iter(validation_ds))
len(X_test)
model = tf.keras.Sequential([
  base_model,
  global_average_layer,
  prediction_layer
])

base_learning_rate = 0.001
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()

initial_epochs = 10
earlystopping = EarlyStopping(monitor='val_loss', patience=6)

batch_size = 100
print('fitting generator...')

print('training size',len(X_training))
history = model.fit(datagen.flow(X_training, Y_training, batch_size=batch_size),
                    steps_per_epoch=len(X_training) / batch_size,
                    epochs=initial_epochs,
                    validation_data=validation_ds,
                    use_multiprocessing=False,
                    )
loss, accuracy = model.evaluate(X_test, Y_test, verbose=True)
print("Testing Accuracy:  {:.4f}".format(accuracy))
model.save(os.path.join(base_path,'model.h5'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1139728249847901, 'fraction_numerical': 0.012573514500101399, 'mean_word_length': 3.892857142857143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '221835', 'license': 'Apache-2.0', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1627, 'n_words': 290, 'path': 'training_model.ipynb', 'repo_name': 'samyon7/Mask_Object_Detection', 'size': '4931'}