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
%tensorflow_version 1.x | |
class pkg: | |
#### DOWNLOADING AND LOADING DATA | |
def get_metadata(metadata_path, which_splits = ['train', 'test']): | |
metadata = pd.read_csv(metadata_path) | |
keep_idx = metadata['split'].isin(which_splits) | |
return metadata[keep_idx] | |
def get_data_split(split_name, flatten, all_data, metadata, image_shape): |
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
# get a table with information about ALL of our images | |
metadata = get_metadata() | |
# what does it look like? | |
metadata.head() |
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
print(metadata.groupby(["class","split"]).count()) | |
sns.countplot(x ="class", data=metadata, hue = "split") |
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
def plot_one_image(data, labels = [], index = None, image_shape = [64,64,3]): | |
num_dims = len(data.shape) | |
num_labels = len(labels) | |
# reshape data if necessary | |
if num_dims == 1: | |
data = data.reshape(target_shape) | |
if num_dims == 2: | |
data = data.reshape(np.vstack[-1, image_shape]) |
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
train_data, train_labels = get_train_data() | |
test_data, test_labels = get_test_data() | |
plot_one_image(train_data,train_labels,1) | |
plot_one_image(train_data,train_labels,40) |
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
### Here we load the train and test data for you to use. | |
(train_data, train_labels) = get_train_data(flatten = True) | |
(test_data, test_labels) = get_test_data(flatten = True) | |
############ | |
knn = KNeighborsClassifier(n_neighbors=10) | |
log_reg = LogisticRegression() | |
knn.fit(train_data, train_labels) |
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
def plot_acc(history, ax = None, xlabel = 'Epoch #'): | |
history = history.history | |
history.update({'epoch':list(range(len(history['val_accuracy'])))}) | |
history = pd.DataFrame.from_dict(history) | |
best_epoch = history.sort_values(by = 'val_accuracy', \ | |
ascending = False).iloc[0]['epoch'] | |
if not ax: | |
f, ax = plt.subplots(1,1) | |
sns.lineplot(x = 'epoch', y = 'val_accuracy', data = history \ | |
label = 'Validation', ax = ax) |
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
model = Sequential() | |
model.add(Reshape((64,64,3))) | |
model.add(Conv2D(32, (3, 3), padding = 'same', \ | |
activation="relu",input_shape = (2000,64,64,3))) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.5)) | |
model.add(Conv2D(32, (3, 3), padding = 'same',activation="relu")) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Flatten()) | |
model.add(Dropout(0.5)) |
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
expert_conv = VGG16(weights = 'imagenet', include_top = False,input_shape=(64,64,3)) | |
for layer in expert_conv.layers: | |
trainable = True | |
layer.trainable = trainable | |
expert_model = Sequential() | |
expert_model.add(Reshape((64,64,3))) | |
expert_model.add(expert_conv) | |
expert_model.add(GlobalAveragePooling2D()) |
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
# Packages used | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
from imblearn.over_sampling import BorderlineSMOTE | |
from sklearn.model_selection import train_test_split | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix | |
from sklearn.linear_model import LogisticRegression |
OlderNewer