-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_one.py
155 lines (140 loc) · 5.7 KB
/
data_one.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import numpy as np
from PIL import Image as image
import csv
import struct
import cv2
import tensorflow as tf
import matplotlib as plt
train_data_dir = 'F:\\pycharm\\汉字\\train'
test_data_dir = 'F:\\pycharm\\汉字\\test1\\'
words = os.listdir(train_data_dir)
test_filename_list= os.listdir(test_data_dir)
category_num=len(words)
img_size = (256, 256)
datasize= img_size[0] * img_size[1]
def loadOneWord(order):
path = train_data_dir + '\\'+ words[order] + '\\'
files = os.listdir(path)
datas = []
for file in files:
file = path + file
img = np.asarray(image.open(file))
img = cv2.resize(img, img_size)
datas.append(img)
datas = np.array(datas)
labels = np.zeros([len(datas), len(words)], dtype=np.uint8)
labels[:, order] = 1
return datas, labels
def transData(): #将所有数据转存,以后就不用每次都从原始数据读取了
num = len(words)
datas = np.array([], dtype=np.uint8)
datas.shape = -1, 256, 256
labels = np.array([], dtype=np.uint8)
labels.shape = -1, 100
for k in range(num):
data, label = loadOneWord(k)
datas = np.append(datas, data, axis=0)
labels = np.append(labels, label, axis=0)
print('loading', k)
np.save('data.npy', datas) #将数据和标签分别存为data和label
np.save('label.npy', labels)
class DataSet():
def __init__(self):
datas = np.load('data.npy')
labels = np.load('label.npy')
index = np.arange(0, 40000, 1, dtype=np.int)
np.random.shuffle(index)
self.data = datas[index]
self.label = labels[index]
def __getitem__(self, index):
return np.reshape(self.data[index],[-1,256,256,1]).astype('float32'), np.reshape(self.label[index],[-1,100]).astype('float32')
def __len__(self):
return len(self.data)
class TestSet():
def __init__(self,transfer=False):
self.files = os.listdir(test_data_dir)
if transfer:
datas = []
for file in self.files:
file = test_data_dir + file
img = np.asarray(image.open(file))
img = cv2.resize(img, img_size)
datas.append(img)
self.data = np.array(datas)
np.save('testdata.npy', self.data)
else:
self.data = np.load('testdata.npy')
self.words = os.listdir(train_data_dir)
def __getitem__(self, index):
return np.reshape(self.data[index], [-1, img_size[0], img_size[0], 1]).astype('float32')
def __len__(self):
return len(self.data)
def label_data(self,predicted_label):
i=0
with open('result.csv','w',encoding='utf-8',newline='') as f:
writer = csv.writer(f)
writer.writerow(['filename','label'])
for file in self.files:
topfive_posible = np.argsort(-predicted_label[i, :])[0:5]
writer.writerow([file,self.words[topfive_posible[0]]+self.words[topfive_posible[1]]+self.words[topfive_posible[2]]+self.words[topfive_posible[3]]+self.words[topfive_posible[4]]])
i=i+1
# def save_csv(test_image_path, predict_label):
#
# save_arr = np.empty((10000, 2), dtype=np.str)
# save_arr = pd.DataFrame(save_arr, columns=['filename', 'lable'])
# predict_label = tran_list2str(predict_label)
# for i in range(len(test_filename_list)):
# filename = test_filename_list[i].split('/')[-1]
# save_arr.values[i, 0] = filename
# save_arr.values[i, 1] = predict_label[i]
# save_arr.to_csv('submit_test.csv', decimal=',', encoding='utf-8', index=False, index_label=False)
# print('submit_test.csv have been write, locate is :', os.getcwd())
def tran_list2str(predict_label):
string_labels=[]
for row in range(len(predict_label)):
index=predict_label[row]
string_labels.append(words[index])
return string_labels
# def transData(): #将所有数据转存,以后就不用每次都从原始数据读取了
# num = len(words)
# datas = np.array([], dtype=np.uint8)
# datas.shape = -1, img_size[0], img_size[1], 1
# labels = np.array([], dtype=np.uint8)
# labels.shape = -1, 100
# for k in range(num):
# data, label = LoadFilesInOneWord(k)
# datas = np.append(datas, data, axis=0)
# labels = np.append(labels, label, axis=0)
# print('loading', k)
# np.save('data.npy', datas) #将数据和标签分别存为data和label
# np.save('label.npy', labels)
# def LoadFilesInOneWord(k):
# path = train_data_dir + '\\' + words[k]
# files = os.listdir(path)
# datas = []
# for file in files:
# file_path=path + '\\' + file
# img = np.asanyarray(image.open(file_path))
# img = cv2.resize(img, img_size)
# datas.append(img)
# datas = np.array(datas)
# datas.shape = len(datas), img_size[0], img_size[1], 1
# labels = np.zeros([len(datas), len(words)], dtype = np.uint8)
# labels[:, k] = 1
# return datas, labels
#
# class TrainSet():
# def __init__(self):
# datas = np.load('data.npy')
# labels = np.load('label.npy')
# index = np.arange(0, len(datas), 1, dtype=np.int)
# np.random.shuffle(index)
# self.data = datas[index]
# self.label = labels[index]
#
# def __getitem__(self, index):
# return self.data[index], self.label[index]
#
# def __len__(self):
# return len(self.data)