<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')# Initialize VGG16 with transfer learning



# ! pip install kaggle
#from google.colab import files
# files.upload()
! mkdir ~/.kaggle
! cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets download -d sriramr/fruits-fresh-and-rotten-for-classification
mkdir data
! unzip fruits-fresh-and-rotten-for-classification.zip -d data
train_path='/content/data/dataset/train'
test_path='/content/data/dataset/test'
#Downloading the pretrained model vggg16
from tensorflow import keras

base_model = keras.applications.VGG16(
    weights='imagenet',  # Load weights pre-trained on ImageNet.
    input_shape=(224, 224, 3),
    include_top=False)
base_model.summary()
# add a global spatial average pooling layer
x = base_model.output

x = keras.layers.GlobalAveragePooling2D()(x)
# and a dense layer
x = keras.layers.Dense(1024, activation='relu')(x)
predictions = keras.layers.Dense(6, activation='softmax')(x)

# this is the model we will train
model = keras.Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional VGG16 layers
for layer in base_model.layers:
    layer.trainable = False

# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer=keras.optimizers.Adam(lr=0.001), metrics=['accuracy'], loss='categorical_crossentropy')
model.summary()
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# create a data generator
train_datagen = ImageDataGenerator(rescale = 1./255,
        samplewise_center=True,  # set each sample mean to 0
        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
        zoom_range = 0.1, # Randomly zoom image 
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False) # we don't expect Bo to be upside-down so we will not flip vertically
test_datagen = ImageDataGenerator(rescale=1./255)
#  load and iterate training dataset
train_it = train_datagen.flow_from_directory(train_path, 
                                       target_size=(224,224), 
                                       color_mode='rgb', 
                                       class_mode="categorical",
                                       batch_size=32)
# load and iterate validation dataset
test_it = test_datagen.flow_from_directory(test_path, 
                                      target_size=(224,224), 
                                      color_mode='rgb', 
                                      class_mode="categorical",
                                      batch_size=32)
                                      
model.fit(train_it,
          validation_data=test_it,
          steps_per_epoch=len(train_it),
          validation_steps=len(test_it),
          epochs=10)
model.evaluate(test_it)<jupyter_output>85/85 [==============================] - 21s 241ms/step - loss: 0.1384 - accuracy: 0.9429
{'fraction_non_alphanumeric': 0.10012360939431397, 'fraction_numerical': 0.02657601977750309, 'mean_word_length': 2.8489892984542213, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '377261', 'license': 'MIT', 'n_tokens_mistral': 1028, 'n_tokens_neox': 931, 'n_words': 235, 'path': 'image_classification.ipynb', 'repo_name': 'UnmilonGeophysics/Image_classification-NVIDIA-Course-Project', 'size': '3236'}