Created
October 14, 2018 12:19
-
-
Save pmarcelino/3e88dcd008a39784e5f8cff4f2826749 to your computer and use it in GitHub Desktop.
Extract features from convolutional base on Dogs vs. Cats Kaggle competition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i want for multiclass