2
$\begingroup$

I am trying to train ResNET50 for dogs and cats classification (Tensorflow2.3) using the following code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from random import shuffle
import os, gc, time, cv2, random, math
import tensorflow as tf
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')

TRAIN_DIR = 'train'
TEST_DIR =  'test'
NUM_CLASSES = 2
IMG_SIZE = 145  ###
CHANNELS = 3
EPOCHS = 15
BATCH_SIZE = 128

train_images = os.listdir(TRAIN_DIR)
test_images = os.listdir(TEST_DIR)

def label_img(img):
    word_label = img.split('.')[-3]
    if word_label == 'cat': return 0  ###
    elif word_label == 'dog' : return 1  ###

def process_data(data_image_list, DATA_FOLDER, isTrain=True):
    data_df = []
    for img in tqdm(data_image_list):
        path = os.path.join(DATA_FOLDER,img)
        if(isTrain):
            label = label_img(img)
        else:
            label = img.split('.')[0]
        img = cv2.imread(path,cv2.IMREAD_COLOR)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        data_df.append([np.array(img), label])
    shuffle(data_df)
    return data_df
train_data = process_data(train_images, TRAIN_DIR, isTrain=True)
X = np.array([i[0] for i in train_data]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y = np.array([i[1] for i in train_data])

import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

def get_pretrained_model(Weights='imagenet', trainable=False, Input_Shape=None):
    input_shape = Input_Shape
    base_model = tf.keras.applications.ResNet50(weights=None, include_top=False, input_shape= input_shape)
#     base_model.load_weights(Weights_path)
    for l in base_model.layers:
        l.trainable = trainable
    return base_model
    
def build_model(PreModel, LearningRate=1e-3, Decay=1e-8):
    
    input_x = PreModel.inputs
    
    x_model = PreModel.output #(None, 1, 1, 2048)
    x_model = tf.keras.layers.Flatten()(x_model)
    
    x_model = tf.keras.layers.Dense(64, activation='relu',name='fc1_Dense')(x_model)
    #x_model = Dropout(0.5, name='dropout_1')(x_model)
    x_model = tf.keras.layers.BatchNormalization()(x_model)
    
    x_model = tf.keras.layers.Dense(32, activation='relu',name='fc2_Dense')(x_model)
    #x_model = Dropout(0.5, name='dropout_2')(x_model)
    x_model = tf.keras.layers.BatchNormalization()(x_model)
    
    predictions = tf.keras.layers.Dense(1, activation='sigmoid',name='output_layer')(x_model)
    model = tf.keras.Model(inputs=input_x, outputs=predictions)
    optimizer = tf.keras.optimizers.Adam(lr=LearningRate, decay=Decay)
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

PreModel = get_pretrained_model(Weights='imagenet',
                                trainable=False,
                                Input_Shape=(IMG_SIZE, IMG_SIZE, CHANNELS))
model = build_model(PreModel, LearningRate=1e-3, Decay=1e-8)
import tensorflow as tf
from sklearn.model_selection import train_test_split


X_train, X_val, y_train, y_val = train_test_split(X,y, test_size=0.2, random_state=1)

# Augmentation configuration to use for training and validation
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        #rescale=1./255,#!!!!!
        shear_range=0.2,
        zoom_range=0.2,
        rotation_range=20, 
        horizontal_flip=True,
        preprocessing_function=tf.keras.applications.resnet50.preprocess_input
)
test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    #rescale=1./255,#!!!!!
    preprocessing_function=tf.keras.applications.resnet50.preprocess_input
)
BestModelWeightsPath = 'BestModel.h5'
check_point = tf.keras.callbacks.ModelCheckpoint(
    BestModelWeightsPath, monitor='val_loss', verbose=1,
    save_best_only=True, 
    save_weights_only=True,
    mode='min'
)
lr_reduce = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.1, min_delta=0.0001, patience=3, verbose=1)
earlyStop =tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=30)
callbacks_list = [check_point, lr_reduce, earlyStop]

tf.keras.backend.set_value(model.optimizer.lr, 0.0001)
gc.collect()
history = model.fit(
    train_datagen.flow(np.array(X_train), y_train, batch_size=BATCH_SIZE, shuffle=True),
    steps_per_epoch= len(X) // BATCH_SIZE,
    validation_data = test_datagen.flow(np.array(X_val), y_val, batch_size=BATCH_SIZE*3, shuffle=False),
    validation_steps = len(X_val) // (BATCH_SIZE*3),
    epochs=EPOCHS,
    shuffle=False,
    verbose=1,
    callbacks=callbacks_list
)

And I got the following warning

WARNING:tensorflow:Your input ran out of data; interrupting training. Make sure that 
your dataset or generator can generate at least `steps_per_epoch * epochs`

I tried to decrease the batch size however still the same error.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.