-
-
Save Diyago/027a303f79a66bdeb9773c6db679d3b0 to your computer and use it in GitHub Desktop.
generate features
This file contains hidden or 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
#export PATH=~/anaconda3/bin:$PATH | |
from tqdm import tqdm | |
import tensorflow as tf | |
from keras.applications.resnet50 import ResNet50 | |
from keras.layers import Flatten, Input | |
from keras.models import Model | |
from keras.preprocessing import image | |
from keras.applications.imagenet_utils import preprocess_input | |
import numpy as np | |
from google.cloud import storage | |
from io import BytesIO | |
model = ResNet50(weights='imagenet', pooling=max, include_top = False) | |
client = storage.Client() | |
bucket = client.get_bucket('nonane') | |
bucket_list = list(bucket.list_blobs()) | |
f = BytesIO(file.download_as_string()) | |
X_test = [] | |
#file creation | |
train_filenames = open('train_filenames.txt', 'w+') | |
test_filenames = open('test_filenames.txt', 'w+') | |
train_featues = open('train_featues.txt', 'w+') | |
test_features = open('test_features.txt', 'w+') | |
i=0 | |
#for file in tqdm(bucket_list[0:5000]): | |
for file in (bucket_list[0:5000]): | |
try: | |
# reading files | |
f = BytesIO(file.download_as_string()) | |
img = image.load_img(f, target_size=(224, 224)) | |
x = image.img_to_array(img) | |
x = np.expand_dims(x, axis=0) | |
x = preprocess_input(x) | |
features = model.predict(x) | |
features_reduce = features.squeeze() | |
X_test.append(features_reduce) | |
#depending on file path write info to proper files | |
file_name = file.path.split('/o/')[1].split('%2F') | |
if file_name[0] == 'train': | |
#print(file.path.split('/o/')[1].split('%2F')[1]) | |
train_filenames.write(file_name[1] + '\n') | |
train_featues.write(' '.join(str(x) for x in features.squeeze()) + '\n') | |
else: | |
test_filenames.write(file_name[1] + '\n') | |
test_features.write(' '.join(str(x) for x in features.squeeze()) + '\n') | |
i = i + 1 | |
print() | |
except: | |
pass | |
print(i) | |
train_filenames.close() | |
test_filenames.close() | |
train_featues.close() | |
test_features.close() | |
#my_file = open('test_filenames.txt', 'r') | |
#print(my_file.read()) | |
#my_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment