Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %tensorflow_version 2.x
- import tensorflow as tf
- print(tf.__version__)
- from tensorflow.keras.applications import ResNet50
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Dense, InputLayer, Flatten, GlobalAveragePooling2D
- num_classes = 2
- IMG_SIZE = 224
- IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
- my_new_model=tf.keras.applications.ResNet50(include_top=False, weights='imagenet', input_shape=IMG_SHAPE, pooling='avg', classes=2)
- my_new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
- !wget --no-check-certificate \
- https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip -O \
- /tmp/cats_and_dogs_filtered.zip
- import os
- import zipfile
- local_zip = '/tmp/cats_and_dogs_filtered.zip'
- zip_ref = zipfile.ZipFile(local_zip, 'r')
- zip_ref.extractall('/tmp')
- zip_ref.close()
- base_dir = '/tmp/cats_and_dogs_filtered'
- train_dir = os.path.join(base_dir, 'train')
- validation_dir = os.path.join(base_dir, 'validation')
- # Directory with our training cat pictures
- train_cats_dir = os.path.join(train_dir, 'cats')
- # Directory with our training dog pictures
- train_dogs_dir = os.path.join(train_dir, 'dogs')
- # Directory with our validation cat pictures
- validation_cats_dir = os.path.join(validation_dir, 'cats')
- # Directory with our validation dog pictures
- validation_dogs_dir = os.path.join(validation_dir, 'dogs')
- train_cat_fnames = os.listdir(train_cats_dir)
- train_dog_fnames = os.listdir(train_dogs_dir)
- from tensorflow.keras.preprocessing.image import ImageDataGenerator
- from tensorflow.keras.applications.resnet50 import preprocess_input
- train_datagen = ImageDataGenerator(
- preprocessing_function=preprocess_input,
- rotation_range=40,
- width_shift_range=0.2,
- height_shift_range=0.2,
- shear_range=0.2,
- zoom_range=0.2,
- horizontal_flip=True,)
- # Note that the validation data should not be augmented!
- test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
- train_generator = train_datagen.flow_from_directory(
- train_dir, # This is the source directory for training images
- target_size=(224,224), # All images will be resized to 224x224
- batch_size=20,
- class_mode='binary')
- validation_generator = test_datagen.flow_from_directory(
- validation_dir,
- target_size=(224, 224),
- class_mode='binary')
- my_new_model.fit_generator(
- train_generator,
- epochs = 8,
- steps_per_epoch=100,
- validation_data=validation_generator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement