Skip to content

Instantly share code, notes, and snippets.

View manashmandal's full-sized avatar
👨‍💻
Probably Coding || ! Probably Coding

Manash Kumar Mandal manashmandal

👨‍💻
Probably Coding || ! Probably Coding
View GitHub Profile
def build_deep_autoencoder(img_shape,code_size=32):
"""PCA's deeper brother. See instructions above"""
H,W,C = img_shape
encoder = keras.models.Sequential()
encoder.add(L.InputLayer(img_shape))
encoder.add(L.Flatten())
encoder.add(L.Dense(code_size*8, activation='relu'))
encoder.add(L.Dense(code_size*4, activation='tanh'))
def visualize(img,encoder,decoder):
"""Draws original, encoded and decoded images"""
code = encoder.predict(img[None])[0]
reco = decoder.predict(code[None])[0]
plt.subplot(1,3,1)
plt.title("Original")
plt.imshow(img)
plt.subplot(1,3,2)
def build_pca_autoencoder(img_shape,code_size=32):
"""
Here we define a simple linear autoencoder as described above.
We also flatten and un-flatten data to be compatible with image shapes
"""
encoder = keras.models.Sequential()
encoder.add(L.InputLayer(img_shape))
encoder.add(L.Flatten()) #flatten image to vector
encoder.add(L.Dense(code_size)) #actual encoder
@manashmandal
manashmandal / lfw_dataset.py
Created January 9, 2018 10:11
Load LFW Dataset
import numpy as np
import os
import cv2
import pandas as pd
import tarfile
import tqdm
ATTRS_NAME = "./data/lfw_attributes.txt" # http://www.cs.columbia.edu/CAVE/databases/pubfig/download/lfw_attributes.txt
RAW_IMAGES_NAME = "./data/lfw.tgz" # http://vis-www.cs.umass.edu/lfw/lfw.tgz
train_X = np.empty((18000, 200, 200))
for i in range(0,18000):
if i >= 3600 and i < 5400:
img = np.rot90(org_train_x[i].reshape(28,28),k =3, axes=(0,1))
img = np.fliplr(img)
elif i >= 9000 and i < 10800:
img = np.rot90(org_train_x[i].reshape(28,28),k =3, axes=(0,1))
img = np.fliplr(img)
import numpy as np
from sklearn.model_selection import train_test_split
def get_train_test(split_ratio=0.6, random_state=42):
# Get available labels
labels, indices, _ = get_labels(DATA_PATH)
# Getting first arrays
X = np.load(labels[0] + '.npy')
y = np.zeros(X.shape[0])
import numpy as np
import os
DATA_PATH = "./data/"
# Input: Folder Path
# Output: Tuple (Label, Indices of the labels, one-hot encoded labels)
def get_labels(path=DATA_PATH):
labels = os.listdir(path)
label_indices = np.arange(0, len(labels))
import numpy as np
import librosa
def wav2mfcc(file_path, max_pad_len=11):
wave, sr = librosa.load(file_path, mono=True, sr=None)
wave = wave[::3]
mfcc = librosa.feature.mfcc(wave, sr=16000)
pad_width = max_pad_len - mfcc.shape[1]
mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
return mfcc
import spacy
import pandas as pd
import argparse
import re
nlp = spacy.load('en')
# Get the name of the file
parser = argparse.ArgumentParser()
parser.add_argument("file_path", help="Enter csv filepath here")