|
a |
|
b/train_unet.py |
|
|
1 |
|
|
|
2 |
# import os |
|
|
3 |
# import numpy as np |
|
|
4 |
# import cv2 |
|
|
5 |
# from tensorflow.keras.models import Model |
|
|
6 |
# from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate |
|
|
7 |
# from tensorflow.keras.optimizers import Adam |
|
|
8 |
# from sklearn.model_selection import train_test_split |
|
|
9 |
|
|
|
10 |
# # --- U-Net Architecture --- |
|
|
11 |
# def build_unet(input_shape): |
|
|
12 |
# inputs = Input(input_shape) |
|
|
13 |
|
|
|
14 |
# # Encoder |
|
|
15 |
# c1 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs) |
|
|
16 |
# c1 = Conv2D(16, (3, 3), activation='relu', padding='same')(c1) |
|
|
17 |
# p1 = MaxPooling2D((2, 2))(c1) |
|
|
18 |
|
|
|
19 |
# c2 = Conv2D(32, (3, 3), activation='relu', padding='same')(p1) |
|
|
20 |
# c2 = Conv2D(32, (3, 3), activation='relu', padding='same')(c2) |
|
|
21 |
# p2 = MaxPooling2D((2, 2))(c2) |
|
|
22 |
|
|
|
23 |
# # Bottleneck |
|
|
24 |
# c3 = Conv2D(64, (3, 3), activation='relu', padding='same')(p2) |
|
|
25 |
# c3 = Conv2D(64, (3, 3), activation='relu', padding='same')(c3) |
|
|
26 |
|
|
|
27 |
# # Decoder |
|
|
28 |
# u1 = UpSampling2D((2, 2))(c3) |
|
|
29 |
# u1 = concatenate([u1, c2]) |
|
|
30 |
# c4 = Conv2D(32, (3, 3), activation='relu', padding='same')(u1) |
|
|
31 |
# c4 = Conv2D(32, (3, 3), activation='relu', padding='same')(c4) |
|
|
32 |
|
|
|
33 |
# u2 = UpSampling2D((2, 2))(c4) |
|
|
34 |
# u2 = concatenate([u2, c1]) |
|
|
35 |
# c5 = Conv2D(16, (3, 3), activation='relu', padding='same')(u2) |
|
|
36 |
# c5 = Conv2D(16, (3, 3), activation='relu', padding='same')(c5) |
|
|
37 |
|
|
|
38 |
# outputs = Conv2D(1, (1, 1), activation='sigmoid')(c5) |
|
|
39 |
|
|
|
40 |
# model = Model(inputs, outputs) |
|
|
41 |
# model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy']) |
|
|
42 |
# return model |
|
|
43 |
|
|
|
44 |
# # --- Load images and masks from all folders --- |
|
|
45 |
# def load_dataset(dataset_path, img_size=(256, 256)): |
|
|
46 |
# images = [] |
|
|
47 |
# masks = [] |
|
|
48 |
# for disease_folder in os.listdir(dataset_path): |
|
|
49 |
# image_path = os.path.join(dataset_path, disease_folder, 'images') |
|
|
50 |
# mask_path = os.path.join(dataset_path, disease_folder, 'mask_image') |
|
|
51 |
# for file in os.listdir(image_path): |
|
|
52 |
# img = cv2.imread(os.path.join(image_path, file), cv2.IMREAD_GRAYSCALE) |
|
|
53 |
# mask = cv2.imread(os.path.join(mask_path, file), cv2.IMREAD_GRAYSCALE) |
|
|
54 |
# if img is not None and mask is not None: |
|
|
55 |
# img = cv2.resize(img, img_size) |
|
|
56 |
# mask = cv2.resize(mask, img_size) |
|
|
57 |
# images.append(img) |
|
|
58 |
# masks.append(mask) |
|
|
59 |
# return np.array(images), np.array(masks) |
|
|
60 |
|
|
|
61 |
# # --- Main --- |
|
|
62 |
# dataset_path = 'dataset/' |
|
|
63 |
# X, Y = load_dataset(dataset_path) |
|
|
64 |
# X = X[..., np.newaxis] / 255.0 |
|
|
65 |
# Y = Y[..., np.newaxis] / 255.0 |
|
|
66 |
|
|
|
67 |
# X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.1, random_state=42) |
|
|
68 |
|
|
|
69 |
# model = build_unet((256, 256, 1)) |
|
|
70 |
# model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=10, batch_size=8) |
|
|
71 |
|
|
|
72 |
# model.save('app/model/unet_model.h5') |
|
|
73 |
# print("U-Net model saved.") |
|
|
74 |
|
|
|
75 |
import os |
|
|
76 |
import numpy as np |
|
|
77 |
import cv2 |
|
|
78 |
from tensorflow.keras.models import Model |
|
|
79 |
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate |
|
|
80 |
from tensorflow.keras.optimizers import Adam |
|
|
81 |
from sklearn.model_selection import train_test_split |
|
|
82 |
|
|
|
83 |
# --- U-Net Architecture --- |
|
|
84 |
def build_unet(input_shape): |
|
|
85 |
inputs = Input(input_shape) |
|
|
86 |
|
|
|
87 |
# Encoder |
|
|
88 |
c1 = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs) |
|
|
89 |
c1 = Conv2D(16, (3, 3), activation='relu', padding='same')(c1) |
|
|
90 |
p1 = MaxPooling2D((2, 2))(c1) |
|
|
91 |
|
|
|
92 |
c2 = Conv2D(32, (3, 3), activation='relu', padding='same')(p1) |
|
|
93 |
c2 = Conv2D(32, (3, 3), activation='relu', padding='same')(c2) |
|
|
94 |
p2 = MaxPooling2D((2, 2))(c2) |
|
|
95 |
|
|
|
96 |
# Bottleneck |
|
|
97 |
c3 = Conv2D(64, (3, 3), activation='relu', padding='same')(p2) |
|
|
98 |
c3 = Conv2D(64, (3, 3), activation='relu', padding='same')(c3) |
|
|
99 |
|
|
|
100 |
# Decoder |
|
|
101 |
u1 = UpSampling2D((2, 2))(c3) |
|
|
102 |
u1 = concatenate([u1, c2]) |
|
|
103 |
c4 = Conv2D(32, (3, 3), activation='relu', padding='same')(u1) |
|
|
104 |
c4 = Conv2D(32, (3, 3), activation='relu', padding='same')(c4) |
|
|
105 |
|
|
|
106 |
u2 = UpSampling2D((2, 2))(c4) |
|
|
107 |
u2 = concatenate([u2, c1]) |
|
|
108 |
c5 = Conv2D(16, (3, 3), activation='relu', padding='same')(u2) |
|
|
109 |
c5 = Conv2D(16, (3, 3), activation='relu', padding='same')(c5) |
|
|
110 |
|
|
|
111 |
outputs = Conv2D(1, (1, 1), activation='sigmoid')(c5) |
|
|
112 |
|
|
|
113 |
model = Model(inputs, outputs) |
|
|
114 |
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy']) |
|
|
115 |
return model |
|
|
116 |
|
|
|
117 |
# --- Load dataset --- |
|
|
118 |
def load_dataset(dataset_path, img_size=(256, 256)): |
|
|
119 |
images, masks = [], [] |
|
|
120 |
|
|
|
121 |
covid_base = os.path.join(dataset_path, 'COVID-19_Radiography_Dataset') |
|
|
122 |
|
|
|
123 |
for disease_folder in os.listdir(covid_base): |
|
|
124 |
disease_path = os.path.join(covid_base, disease_folder) |
|
|
125 |
if not os.path.isdir(disease_path): |
|
|
126 |
continue |
|
|
127 |
|
|
|
128 |
image_path = os.path.join(disease_path, 'images') |
|
|
129 |
mask_path = os.path.join(disease_path, 'masks') |
|
|
130 |
|
|
|
131 |
if not os.path.exists(image_path) or not os.path.exists(mask_path): |
|
|
132 |
continue |
|
|
133 |
|
|
|
134 |
for file in os.listdir(image_path): |
|
|
135 |
if file.startswith('.'): |
|
|
136 |
continue |
|
|
137 |
|
|
|
138 |
img_file = os.path.join(image_path, file) |
|
|
139 |
mask_file = os.path.join(mask_path, file) |
|
|
140 |
|
|
|
141 |
img = cv2.imread(img_file, cv2.IMREAD_GRAYSCALE) |
|
|
142 |
mask = cv2.imread(mask_file, cv2.IMREAD_GRAYSCALE) |
|
|
143 |
|
|
|
144 |
if img is not None and mask is not None: |
|
|
145 |
img = cv2.resize(img, img_size) |
|
|
146 |
mask = cv2.resize(mask, img_size) |
|
|
147 |
images.append(img) |
|
|
148 |
masks.append(mask) |
|
|
149 |
|
|
|
150 |
return np.array(images), np.array(masks) |
|
|
151 |
|
|
|
152 |
# --- Main --- |
|
|
153 |
dataset_path = '/kaggle/input/lungs-dataset' |
|
|
154 |
X, Y = load_dataset(dataset_path) |
|
|
155 |
|
|
|
156 |
X = X[..., np.newaxis] / 255.0 |
|
|
157 |
Y = Y[..., np.newaxis] / 255.0 |
|
|
158 |
|
|
|
159 |
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.1, random_state=42) |
|
|
160 |
|
|
|
161 |
model = build_unet((256, 256, 1)) |
|
|
162 |
model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=10, batch_size=8) |
|
|
163 |
|
|
|
164 |
model.save('/kaggle/working/unet_model.h5') |
|
|
165 |
print("U-Net model saved as unet_model.h5") |