-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
127 lines (100 loc) · 3.18 KB
/
model.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
import tensorflow as tf
img_path = 'D:/kaggle/feiyan/postcrop_train/NORMAL/1.png'
img_raw = tf.io.gfile.GFile(img_path, 'rb').read()
img_tensor = tf.image.decode_image(img_raw)
print(img_tensor.shape)
print(img_tensor.dtype)
'''
根据模型调整大小
'''
img_final = tf.image.resize(img_tensor,[128,128])
img_final = img_final/255.0
print(img_final.shape)
print(img_final.numpy().min())
print(img_final.numpy().max())
'''
把这些包装再一个简单的函数里
'''
def preprocess_image(image):
'''
image=img_raw
'''
image = tf.image.decode_png(image, channels=3)
image = tf.image.resize(image, [128, 128])
image /= 255.0 # 归一化
return image
def load_and_preprocess_image(path):
'''
param:img_path
return:img_raw
'''
image = tf.io.gfile.GFile(img_path, 'rb').read()
return preprocess_image(image)
'''
所有图片的path
list
'''
import os
all_image_paths = os.listdir('D:/kaggle/feiyan/postcrop_train/NORMAL/')
all_image_paths = all_image_paths+(os.listdir('D:/kaggle/feiyan/postcrop_train/PNEUMONIA/'))
print(len(all_image_paths))
#from_tensor_slices:将字符串数组切片,得到一个字符串数据集
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
print(path_ds)
'''
来动态加载每一个图片进入函数load_and_preprocess_image
'''
image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)
'''
添加一个标签数据集
'''
label_names = {0: 'NORMAL', 1: 'PNEUMONIA'}
all_image_labels = []
for i in range(1341):
all_image_labels.append(0)
for i in range(1341, 5216):
all_image_labels.append(1)
print(len(all_image_labels))
# print(all_image_labels[1340],all_image_labels[1341])
label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, tf.int64))
print(label_ds)
for label in label_ds.take(10):
print(label_names[label.numpy()])
'''
image_ds 和 label_ds打包成一个(图片,标签)数据集
格式:ZipDataset
'''
image_label_ds = tf.data.Dataset.zip((image_ds,label_ds))
print(image_label_ds)
'''
1.打乱数据
2.分割数据
3.重复数据
4.在处理当前元素时准备后面的元素
'''
BATCH_SIZE = 32
image_count = 5216
# 设置一个和数据集大小一致的 shuffle buffer size(随机缓冲区大小)以保证数据
# 被充分打乱。
ds = image_label_ds.shuffle(buffer_size=image_count)
ds = ds.repeat()
ds = ds.batch(BATCH_SIZE)
#这允许在处理当前元素时准备后面的元素
ds = ds.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
ds
from tensorflow.keras import datasets, layers, models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(2))
a
# model.summary()
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(ds, epochs=10,steps_per_epoch=12)