Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli separation and data parsing #23

Closed
wants to merge 9 commits into from
32 changes: 25 additions & 7 deletions features.py
Expand Up @@ -8,9 +8,9 @@

import os

from keras import applications
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
from tensorflow.keras import applications, models, Model
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
import numpy as np
from pandas import DataFrame as DF

Expand Down Expand Up @@ -47,7 +47,7 @@ def _extract(fp, model):
# Load the image, setting the size to 224 x 224
img = image.load_img(fp, target_size=(224, 224))

# Convert the image to a numpy array, resize it (1, 2, 244, 244), and preprocess it
# Convert the image to a numpy array, resize it (1, 224, 224, 3), and preprocess it
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
Expand All @@ -67,9 +67,27 @@ def extract_features(filepath, model='ResNet50', write_to=None):
print('Extracting features')

# Get the model
print('Acquiring model "{}"'.format(model), end='')
m = named_model(model)
print('\rAcquired model\t\t\t\t\t')
print('Acquiring model "{}"'.format(model))

if type(model) == str:

# From filepath
if os.path.exists(model):
print('Assuming model argument is a filepath')
m = models.load_model(model)

# From standard named models
else:
print('Assuming model argument is a named model')
m = named_model(model)

# Model already in memory
else:
print('Assuming model argument is a loaded model')
m = model

assert isinstance(m, Model), 'Model \'{}\' is not a tf.keras.Model'.format(model)
print('\rSuccessfully acquired model\t\t\t\t\t')

# Get the image filepaths
filepath = filepath.replace('\\', '/')
Expand Down
2 changes: 1 addition & 1 deletion tsne_reducer.py
Expand Up @@ -23,7 +23,7 @@ def tsne(features, dims=2, write_to=None, tsne_kwargs={}):
# Don't consider the first unique ID column
features_salient = features.copy().drop(columns=[id_col_name], axis=1)

reduced = pd.DataFrame(TSNE(**tsne_kwargs).fit_transform(features_salient))
reduced = pd.DataFrame(TSNE(**tsne_kwargs).fit_transform(features_salient), dtype=object)
reduced.insert(0, id_col_name, features[[id_col_name]])

print('Success')
Expand Down
2 changes: 1 addition & 1 deletion umap_reducer.py
Expand Up @@ -25,7 +25,7 @@ def umap(features, dims=2, write_to=None):
# Don't consider the first unique ID column
features_salient = features.copy().drop(columns=[id_col_name], axis=1)

reduced = pd.DataFrame(UMAP(spread=0.5).fit_transform(features_salient))
reduced = pd.DataFrame(UMAP(spread=0.5).fit_transform(features_salient), dtype=object)
reduced.insert(0, id_col_name, features[[id_col_name]])

print('Success')
Expand Down