<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
CUR_DIR = '/content/drive/My Drive/Race'
from __future__ import print_function
import tensorflow as tf
import numpy as np
import os
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>1.Check dataset:<jupyter_code>RACE_LIST = ['Asian', 'Black', 'India', 'Others', 'White']
total = 0
class_weight = dict()
number_class = dict()
for idx, race in enumerate(RACE_LIST):
  class_weight[idx] = 0
for idx, race in enumerate(RACE_LIST):
  print(race, " has ", len(os.listdir(CUR_DIR + '//train//' + race)), " images in training set")
  print(race, " has ", len(os.listdir(CUR_DIR + '//test//' + race)), " images in testing set")
  class_weight[idx] = len(os.listdir(CUR_DIR + '//train//' + race)) + len(os.listdir(CUR_DIR + '//test//' + race))
  total += class_weight[idx]
for idx, race in enumerate(RACE_LIST):
  class_weight[idx] /= total
TRAINING_DIR = CUR_DIR + "//train//"
TESTING_DIR = CUR_DIR + "//test//"<jupyter_output><empty_output><jupyter_text>2.Create Image Generator from directory:<jupyter_code>from tensorflow.keras.preprocessing.image import ImageDataGenerator
TARGET_SIZE = (200, 200)
BATCH_SIZE = 128
train_datagen = ImageDataGenerator(rescale=1./255.)
test_datagen = ImageDataGenerator(rescale=1./255.)

train_generator = train_datagen.flow_from_directory(
    TRAINING_DIR,
    batch_size=BATCH_SIZE,
    class_mode='categorical',
    target_size=TARGET_SIZE
)
test_generator = test_datagen.flow_from_directory(
    TESTING_DIR,
    batch_size=BATCH_SIZE,
    class_mode='categorical',
    target_size=TARGET_SIZE
)<jupyter_output>Found 8798 images belonging to 5 classes.
Found 981 images belonging to 5 classes.<jupyter_text>3.Create Model:<jupyter_code>from tensorflow.keras.applications.vgg16 import VGG16
NUM_CLASSES = 5
pretrained_model = VGG16(
    include_top = False,
    weights=None,
    input_shape=(200, 200, 3)
)
for layer in pretrained_model.layers:
  layer.trainable = False
pretrained_model.summary()
last_layer = pretrained_model.get_layer('block5_pool')
last_output = last_layer.output
print(last_output.shape)
net = tf.keras.layers.Flatten()(last_output)
net = tf.keras.layers.Dense(1024, activation=tf.nn.relu)(net)
net = tf.keras.layers.Dropout(0.2)(net)
net = tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')(net)

model = tf.keras.Model(pretrained_model.input, net)
model.summary()<jupyter_output>Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 200, 200, 3)]     0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 200, 200, 64)      1792      
_________________________________________________________________
block1_conv2 (Conv2D)        (None, 200, 200, 64)      36928     
_________________________________________________________________
block1_pool (MaxPooling2D)   (None, 100, 100, 64)      0         
_________________________________________________________________
block2_conv1 (Conv2D)        (None, 100, 100, 128)     73856     
_________________________________________________________________
block2_conv2 (Conv2D)        (None, 100, 100, 128)     147584    
_____________________________________________________________[...]<jupyter_text>4.Training the model:<jupyter_code>EPOCHS = 100
STEPS_PER_EPOCH = 70
VALIDATION_STEPS = 8
LR=0.001
mc = tf.keras.callbacks.ModelCheckpoint(filepath="model.h5", 
                                     monitor='val_acc',
                                     mode='max', 
                                     verbose=0, 
                                     save_best_only=True)
adam = tf.keras.optimizers.Adam(lr=LR, decay=LR/EPOCHS)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['acc'])
history = model.fit_generator(
    train_generator,
    steps_per_epoch=STEPS_PER_EPOCH,
    epochs=EPOCHS,
    verbose=2,
    validation_data=test_generator,
    callbacks=[mc],
    class_weight=class_weight
)
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
range_epochs = range(1, EPOCHS + 1)
plt.plot(range_epochs, loss_values, 'bo', label='Training loss')
plt.plot(range_epochs, val_loss_values, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
acc_values = history_dict['acc']
val_acc_values = history_dict['val_acc']
plt.plot(range_epochs, acc_values, 'bo', label='Training acc')
plt.plot(range_epochs, val_acc_values, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12761633814265394, 'fraction_numerical': 0.031294452347083924, 'mean_word_length': 4.783783783783784, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '450667', 'license': 'MIT', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1559, 'n_words': 298, 'path': 'Pretrained.ipynb', 'repo_name': 'hllj/RaceRegconitionVGG16', 'size': '4921'}