Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shayaf84
shayaf84 / Imports.py
Last active August 28, 2020 16:25
Importing Data used for Project
%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):
@shayaf84
shayaf84 / metadata_table.py
Created July 30, 2020 16:30
Displays a table that gives us information about the data
# get a table with information about ALL of our images
metadata = get_metadata()
# what does it look like?
metadata.head()
@shayaf84
shayaf84 / snsplots.py
Created July 30, 2020 16:54
Graph to analyze Data
print(metadata.groupby(["class","split"]).count())
sns.countplot(x ="class", data=metadata, hue = "split")
@shayaf84
shayaf84 / plot_image.py
Last active August 28, 2020 16:27
Function that displays an image
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])
@shayaf84
shayaf84 / images.py
Last active July 31, 2020 15:59
Run the function plot_one_image (access it in plot_image.py) to plot one image
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)
@shayaf84
shayaf84 / ML_Models.py
Last active August 1, 2020 16:41
KNN, and Logistic Regression
### 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)
@shayaf84
shayaf84 / plot_acc_loss.py
Last active August 2, 2020 23:39
Plot accuracy and loss
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)
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))
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())
# 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