3

I want to maintain the first 4 layers of vgg 16 and add the last layer. I have this example:

vgg16_model = VGG16(weights="imagenet", include_top=True)

(2) remove the top layer

base_model = Model(input=vgg16_model.input, output=vgg16_model.get_layer("block5_pool").output) #I wanna cut all layers after 'block1_pool'

(3) attach a new top layer

base_out = base_model.output base_out = Reshape(25088,)(base_out) top_fc1 = Dropout(0.5)(base_out) top_preds = Dense(1, activation="sigmoid")(top_fc1)

(4) freeze weights until the last but one convolution layer (block4_pool)

for layer in base_model.layers[0:4]: layer.trainable = False

(5) create new hybrid model

model = Model(input=base_model.input, output=top_preds)

So in this example he is cutting from the 'block5_pool', and I want to cut from 'block1_pool' but if I only change to block1_pool it throws this error:

data_format = value.lower()

AttributeError: 'int' object has no attribute 'lower'

So how could I change it to cut in block1_pool, and then add my own dense layers?

FULL CODE

#import tensorflow as tf
import cv2
import os
import numpy as np

from keras.layers.core import Flatten, Dense, Dropout, Reshape from keras.models import Model from keras.layers import Input, ZeroPadding2D, Dropout from keras import optimizers from keras.optimizers import SGD from keras.preprocessing.image import ImageDataGenerator from keras.callbacks import EarlyStopping

from keras.applications.vgg16 import VGG16

TRAIN_DIR = 'train/' TEST_DIR = 'test/' v = 'v/' BATCH_SIZE = 32 NUM_EPOCHS = 5

def crop_img(img, h, w): h_margin = (img.shape[0] - h) // 2 if img.shape[0] > h else 0 w_margin = (img.shape[1] - w) // 2 if img.shape[1] > w else 0

crop_img = img[h_margin:h + h_margin,w_margin:w + w_margin,:]

return crop_img

def subtract_gaussian_blur(img):

return cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), 5), -4, 128)

def ReadImages(Path): LabelList = list() ImageCV = list() classes = ["nonPdr", "pdr"]

# Get all subdirectories
FolderList = [f for f in os.listdir(Path) if not f.startswith('.')]

# Loop over each directory
for File in FolderList:
    for index, Image in enumerate(os.listdir(os.path.join(Path, File))):
        # Convert the path into a file
        ImageCV.append(cv2.resize(cv2.imread(os.path.join(Path, File) + os.path.sep + Image), (224,224)))
        #ImageCV[index]= np.array(ImageCV[index]) / 255.0
        LabelList.append(classes.index(os.path.splitext(File)[0])) 

        img_crop = crop_img(ImageCV[index].copy(), 224, 224)

        ImageCV[index] = subtract_gaussian_blur(img_crop.copy())

return ImageCV, LabelList


data, labels = ReadImages(TRAIN_DIR) valid, vlabels = ReadImages(TEST_DIR)

vgg16_model = VGG16(weights="imagenet", include_top=True)

(2) remove the top layer

base_model = Model(input=vgg16_model.input, output=vgg16_model.get_layer("block1_pool").output) print(base_model)

(3) attach a new top layer

base_out = base_model.output base_out = Reshape(25088,)(base_out) top_fc1 = Dropout(0.5)(base_out)

output layer: (None, 5)

top_preds = Dense(1, activation="sigmoid")(top_fc1)

(4) freeze weights until the last but one convolution layer (block4_pool)

for layer in base_model.layers[0:4]: layer.trainable = False

(5) create new hybrid model

model = Model(input=base_model.input, output=top_preds)

(6) compile and train the model

sgd = SGD(lr=1e-4, momentum=0.9) model.compile(optimizer=sgd, loss="binary_crossentropy", metrics=["accuracy"])

data = np.asarray(data) valid = np.asarray(valid)

data = data.astype('float32') valid = valid.astype('float32')

data /= 255 valid /= 255 labels = np.array(labels)

datagen = ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True)

compute quantities required for featurewise normalization

(std, mean, and principal components if ZCA whitening is applied)

datagen.fit(data) mean = datagen.mean
std = datagen.std

print(mean, "mean") print(std, "std")

es = EarlyStopping(monitor='val_loss', verbose=1)

fits the model on batches with real-time data augmentation:

model.fit_generator(datagen.flow(data, np.array(labels), batch_size=32), steps_per_epoch=len(data) / 32, epochs=15, validation_data=(valid, np.array(vlabels)), nb_val_samples=72, callbacks=[es])

model.save('model.h5')

FULL ERROR

    base_out = Reshape(25088,)(base_out)
self.target_shape = tuple(target_shape)

TypeError: 'int' object is not iterable ```

0nroth1
  • 241
  • 2
  • 12

1 Answers1

1

The solution is include Flatten layer to the model:

base_out = base_model.output
top_fc1 = Flatten()(base_out)
top_fc2 = Dropout(0.5)(top_fc1)
top_preds = Dense(1, activation="sigmoid")(top_fc2)

Now it works!

0nroth1
  • 241
  • 2
  • 12