Skip to content

Instantly share code, notes, and snippets.

@pmarcelino
Created October 14, 2018 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarcelino/3e88dcd008a39784e5f8cff4f2826749 to your computer and use it in GitHub Desktop.
Save pmarcelino/3e88dcd008a39784e5f8cff4f2826749 to your computer and use it in GitHub Desktop.
Extract features from convolutional base on Dogs vs. Cats Kaggle competition
# Extract features
import os, shutil
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 32
def extract_features(directory, sample_count):
features = np.zeros(shape=(sample_count, 7, 7, 512)) # Must be equal to the output of the convolutional base
labels = np.zeros(shape=(sample_count))
# Preprocess data
generator = datagen.flow_from_directory(directory,
target_size=(img_width,img_height),
batch_size = batch_size,
class_mode='binary')
# Pass data through convolutional base
i = 0
for inputs_batch, labels_batch in generator:
features_batch = conv_base.predict(inputs_batch)
features[i * batch_size: (i + 1) * batch_size] = features_batch
labels[i * batch_size: (i + 1) * batch_size] = labels_batch
i += 1
if i * batch_size >= sample_count:
break
return features, labels
train_features, train_labels = extract_features(train_dir, train_size) # Agree with our small dataset size
validation_features, validation_labels = extract_features(validation_dir, validation_size)
test_features, test_labels = extract_features(test_dir, test_size)
@shahzaibarif12
Copy link

i want for multiclass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment