-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarmup1.py
52 lines (42 loc) · 1.41 KB
/
warmup1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""Untitled
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1JDQjeuRBusQaEUEwkTGnq_SrS4IzlMUE
"""
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import matplotlib
matplotlib.use("Agg")
import keras
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np
import pandas as pd
model = VGG16(weights = 'imagenet')
val_path = '/mnt/fast-data16/datasets/ILSVRC/2012/clsloc/val/'
n_val = len(os.listdir(val_path))
print('num of validation files: ', n_val)
classes = os.listdir(val_path)
val_path_list = []
for class_file in classes:
path = val_path + class_file + '/'
images_files = os.listdir(path)
for image_file in images_files:
val_path_list.append(path+ image_file)
def get_prediction(file_path):
img = image.load_img(file_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
preds = model.predict(preprocess_input(x))
return decode_predictions(preds, top=5)[0]
results = []
for i in range(len(val_path_list)):
if i%1000 == 0:
print ('now predicting', i, 'th image')
results.append(get_prediction(val_path_list[i]))
save_path = '/home/xiao/warmup1'
df = pd.DataFrame(results)
df.to_csv(os.path.join(save_path, 'warmup1_prediction.csv'))
print ('YEAH!')