Created
March 21, 2020 16:59
-
-
Save miki998/6b6ad05239fa9d39a35c8e9b9b7a21a3 to your computer and use it in GitHub Desktop.
data extract for hyperas
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 data(): | |
""" | |
Data providing function: | |
This function is separated from create_model() so that hyperopt | |
won't reload data for each evaluation run. | |
""" | |
#Encodings | |
encode = {0:'Attire', | |
1:'Decorationandsignage', | |
2:'Food', | |
3: 'misc'} | |
reverse = {v: k for k, v in encode.items()} | |
#Loading | |
PATH = 'train_images/' | |
image_names = os.listdir(PATH) | |
train = pd.read_csv('train.csv') | |
images = [cv2.imread(PATH+image_name) for image_name in tqdm(image_names)] | |
labels = [train.loc[train['Image'] == image_name]['Class'].iloc[0] for image_name in tqdm(image_names)] | |
def standard_image(img): | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
return cv2.resize(img,(48,48),interpolation=cv2.INTER_AREA).reshape((48,48,1)) | |
#Encoding and preparation for training | |
X = np.array(images) | |
y_raw = np.array([reverse[label] for label in labels]) | |
y = [] | |
for cls in y_raw: | |
init = np.zeros(4) | |
init[cls] = 1 | |
y.append(init) | |
y = np.array(y) | |
X.shape,y.shape | |
np.random.seed(40) | |
ratio = 0.8 | |
indexes = range(len(images)) | |
train_idx = random.sample(indexes,int(0.8*len(images))) | |
test_idx = np.delete(indexes, train_idx) | |
#Cross Validation | |
X_train_raw, y_train = X[train_idx], y[train_idx] | |
X_test_raw, y_test = X[test_idx], y[test_idx] | |
#Standardize_all elements | |
X_train = np.array([standard_image(X_train_raw[i]) for i in range(len(X_train_raw))]) | |
X_test = np.array([standard_image(X_test_raw[i]) for i in range(len(X_test_raw))]) | |
return X_train, y_train, X_test, y_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment