Skip to content

Commit cb3c41f

Browse files
committed
add graph models
1 parent 187531c commit cb3c41f

13 files changed

+342
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
'''
3+
Create a GAN-based node that self-generates an instance of image from the model
4+
'''
5+
if __name__ == "__main__":
6+
root_path="models/gan_calendula"
7+
data_path="../../datasets/Flower_MNIST_JPG/calendula/0003.jpg"
8+
gnode=GanNode(root_path=root_path, dim=64)
9+
# gnode.create_samples_from(src_img_path=data_path)
10+
# gnode.build_model(iterations=1000)
11+
gnode.export_performance()
12+
gnode.generate(n_samples=5,show=True)
13+
14+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from mmkfeatures.graph.cnn_node import CnnNode
2+
from PIL import Image
3+
import numpy as np
4+
5+
'''
6+
Create a CNN-based model that classifies an image into a label
7+
'''
8+
if __name__=="__main__":
9+
cnn_node=CnnNode(root_path="models",name="flower_cnn")
10+
cnn_node.build(n_epochs=1000,train_dir="../../datasets/Flower_MNIST_JPG",use_file_names=True)
11+
cnn_node.show_history()
12+
# clear
13+
X = Image.open("../../datasets/Flower_MNIST_JPG/rose/0006.jpg")
14+
X = np.array(X)
15+
img = np.resize(X,(64,64,3))
16+
img = np.reshape(img, (img.shape[0], img.shape[1], 3))
17+
print(X.shape)
18+
cnn_node.predict(img,channels=3,show_fig=True)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cnn_node import CnnNode
3+
from PIL import Image
4+
import numpy as np
5+
6+
7+
'''
8+
link the model between GAN node and CNN nodes
9+
'''
10+
if __name__ == "__main__":
11+
root_path="models/gan_calendula"
12+
data_path="../../datasets/Flower_MNIST_JPG/calendula/0003.jpg"
13+
gnode=GanNode(root_path=root_path, dim=64)
14+
# gnode.create_samples_from(src_img_path=data_path)
15+
# gnode.build_model(iterations=1000)
16+
# gnode.export_performance()
17+
X=gnode.generate(n_samples=10)
18+
img0=X[0]
19+
20+
cnn_node = CnnNode(root_path="models", name="flower_cnn")
21+
# cnn_node.build(n_epochs=10, train_dir="data/Flower_MNIST", use_file_names=True)
22+
# clear
23+
# X = Image.open("data/Flower_MNIST/rose/0006.jpg")
24+
# X = np.array(X)
25+
img = np.resize(img0, (64, 64, 3))
26+
img = np.reshape(img, (img.shape[0], img.shape[1], 3))
27+
print(X.shape)
28+
cnn_node.predict(img, channels=3,show_fig=True)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cnn_node import CnnNode
3+
from PIL import Image
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
'''
8+
evaluate the model based on bridging the input and output of models
9+
'''
10+
if __name__ == "__main__":
11+
label="calendula"
12+
root_path="models/gan_calendula"
13+
data_path=f"../../datasets/Flower_MNIST_JPG/{label}/0003.jpg"
14+
gnode=GanNode(root_path=root_path, dim=64)
15+
# gnode.create_samples_from(src_img_path=data_path)
16+
# gnode.build_model(iterations=1000)
17+
# gnode.export_performance()
18+
X=gnode.generate(n_samples=10)
19+
20+
cnn_node = CnnNode(root_path="models", name="flower_cnn")
21+
# cnn_node.build(n_epochs=10, train_dir="data/Flower_MNIST", use_file_names=True)
22+
list_result=[]
23+
print("len(X) = ",len(X))
24+
num_correct=0
25+
num_total=len(X)
26+
for img in X:
27+
# X = Image.open("data/Flower_MNIST/rose/0006.jpg")
28+
# X = np.array(X)
29+
# img = np.resize(img, (64, 64, 3))
30+
# img = np.reshape(img, (img.shape[0], img.shape[1], 3))
31+
predicted=cnn_node.predict(img, channels=3,show_fig=False)
32+
if label==predicted:
33+
num_correct+=1
34+
35+
list_result.append(predicted)
36+
37+
# results
38+
print("the results: ")
39+
for x in list_result:
40+
print(x)
41+
print("correct rate: ",round(num_correct*1.0/num_total,4))
42+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cgan_node import CGanNode
3+
from mmkfeatures.graph.cnn_node import CnnNode
4+
from PIL import Image
5+
import numpy as np
6+
import cv2
7+
from keras.preprocessing import image
8+
import os
9+
10+
if __name__=="__main__":
11+
label="HeadCT"
12+
root_path = f"models/{label}"
13+
gnode = GanNode(root_path=root_path, dim=64)
14+
# gnode.create_samples_from(src_img_path="../../datasets/Medical_MNIST/HeadCT/000000.jpeg",use_gray=True)
15+
# gnode.build_model(iterations=1000,use_gray=True)
16+
# gnode.export_performance()
17+
18+
X=gnode.generate(n_samples=10) # generated image
19+
img0=X[0]
20+
print(X[9])
21+
22+
# img0=np.array(X[0])
23+
img0=np.resize(img0,(64,64,1))
24+
# img0 = np.reshape(img0, (img0.shape[0], img0.shape[1], 1))
25+
img = image.array_to_img(img0 * 255., scale=False)
26+
img.save('savedimage.png')
27+
28+
cnn_node = CnnNode(root_path="models", name="medical_cnn")
29+
30+
# cnn_node.build(n_epochs=10,train_dir="../../datasets/Medical_MNIST")
31+
# clear
32+
img0 = Image.open("savedimage.png")
33+
img0 = np.array(img0)
34+
img0 = np.reshape(img0, (img0.shape[0], img0.shape[1], 1))
35+
36+
predicted=cnn_node.predict(img0,show_fig=False)
37+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mmkfeatures.graph.cnn_node import CnnNode
2+
from PIL import Image
3+
import numpy as np
4+
5+
if __name__=="__main__":
6+
cnn_node=CnnNode(root_path="models",name="medical_cnn")
7+
cnn_node.build(n_epochs=10,train_dir="../../datasets/Medical_MNIST")
8+
# clear
9+
X = Image.open("../../datasets/Medical_MNIST/ChestCT/000000.jpeg")
10+
X = np.array(X)
11+
img = np.reshape(X, (X.shape[0], X.shape[1], 1))
12+
cnn_node.predict(img)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cnn_node import CnnNode
3+
from PIL import Image
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
'''
8+
evaluate the model based on bridging the input and output of models
9+
'''
10+
if __name__ == "__main__":
11+
label="HeadCT"
12+
root_path=f"models/{label}"
13+
# data_path=f"../../datasets/Medical_MNIST/{label}/0003.jpg"
14+
gnode=GanNode(root_path=root_path, dim=64)
15+
# gnode.create_samples_from(src_img_path=data_path)
16+
# gnode.build_model(iterations=1000)
17+
# gnode.export_performance()
18+
X=gnode.generate(n_samples=10)
19+
20+
cnn_node = CnnNode(root_path="models", name="medical_cnn")
21+
# cnn_node.build(n_epochs=10, train_dir="data/Flower_MNIST", use_file_names=True)
22+
list_result=[]
23+
print("len(X) = ",len(X))
24+
num_correct=0
25+
num_total=len(X)
26+
for img in X:
27+
# X = Image.open("data/Flower_MNIST/rose/0006.jpg")
28+
# X = np.array(X)
29+
# img = np.resize(img, (64, 64, 3))
30+
# img = np.reshape(img, (img.shape[0], img.shape[1], 3))
31+
predicted=cnn_node.predict(img, channels=1,show_fig=False)
32+
if label==predicted:
33+
num_correct+=1
34+
35+
list_result.append(predicted)
36+
37+
# results
38+
print("the results: ")
39+
for x in list_result:
40+
print(x)
41+
print("correct rate: ",round(num_correct*1.0/num_total,4))
42+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
3+
if __name__ == "__main__":
4+
root_path="models/gan_flower_calendula"
5+
data_path="datasets/Flower_MNIST_JPG/calendula/0003.jpg"
6+
gnode=GanNode(root_path=root_path, dim=64)
7+
gnode.create_samples_from(src_img_path=data_path)
8+
gnode.build_model(iterations=1000)
9+
gnode.export_performance()
10+
gnode.generate(n_samples=10)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cgan_node import CGanNode
3+
from mmkfeatures.graph.cnn_node import CnnNode
4+
from PIL import Image
5+
import numpy as np
6+
import cv2
7+
from keras.preprocessing import image
8+
import os
9+
from quickcsv import *
10+
11+
if __name__=="__main__":
12+
list_results=[]
13+
label="HeadCT"
14+
root_path = f"models/{label}"
15+
gnode = GanNode(root_path=root_path, dim=64)
16+
gnode.create_samples_from(src_img_path="datasets/Medical_MNIST/HeadCT/000000.jpeg",use_gray=True)
17+
gnode.build_model(iterations=1000,use_gray=True)
18+
#gnode.export_performance()
19+
20+
X=gnode.generate(n_samples=10) # generated image
21+
img0=X[0]
22+
print(X[9])
23+
24+
# img0=np.array(X[0])
25+
img0=np.resize(img0,(64,64,1))
26+
# img0 = np.reshape(img0, (img0.shape[0], img0.shape[1], 1))
27+
img = image.array_to_img(img0 * 255., scale=False)
28+
img.save('savedimage.png')
29+
30+
cnn_node = CnnNode(root_path="models", name="medical_cnn")
31+
32+
cnn_node.build(n_epochs=10,train_dir="datasets/Medical_MNIST")
33+
# clear
34+
img0 = Image.open("savedimage.png")
35+
img0 = np.array(img0)
36+
img0 = np.reshape(img0, (img0.shape[0], img0.shape[1], 1))
37+
38+
predicted=cnn_node.predict(img0,show_fig=False)
39+
40+
list_results.append({
41+
"label": label,
42+
"predicted": predicted
43+
})
44+
45+
write_csv('outputs/gan_cnn_headct.csv',list_results)
46+
47+
48+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from mmkfeatures.graph.cnn_node import CnnNode
2+
from PIL import Image
3+
import numpy as np
4+
5+
if __name__=="__main__":
6+
cnn_node=CnnNode(root_path="models",name="medical_cnn")
7+
cnn_node.build(n_epochs=10,train_dir="datasets/Medical_MNIST")
8+
# clear
9+
X = Image.open("datasets/Medical_MNIST/ChestCT/000000.jpeg")
10+
X = np.array(X)
11+
img = np.reshape(X, (X.shape[0], X.shape[1], 1))
12+
cnn_node.predict(img)
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from mmkfeatures.graph.cnn_node import CnnNode
2+
from PIL import Image
3+
import numpy as np
4+
5+
if __name__=="__main__":
6+
cnn_node=CnnNode(root_path="models",name="flower_cnn")
7+
cnn_node.build(n_epochs=1000,train_dir="datasets/Flower_MNIST",use_file_names=True)
8+
cnn_node.show_history()
9+
# clear
10+
X = Image.open("datasets/Flower_MNIST/rose/0006.jpg")
11+
X = np.array(X)
12+
img = np.resize(X,(64,64,3))
13+
img = np.reshape(img, (img.shape[0], img.shape[1], 3))
14+
print(X.shape)
15+
cnn_node.predict(img,channels=3)

examples-graph/test5_link_build.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cnn_node import CnnNode
3+
from PIL import Image
4+
import numpy as np
5+
6+
if __name__ == "__main__":
7+
root_path="models/gan_flower_calendula"
8+
data_path="data/Flower_MNIST/calendula/0003.jpg"
9+
gnode=GanNode(root_path=root_path, dim=64)
10+
# gnode.create_samples_from(src_img_path=data_path)
11+
# gnode.build_model(iterations=1000)
12+
# gnode.export_performance()
13+
X=gnode.generate(n_samples=10)
14+
img0=X[0]
15+
16+
cnn_node = CnnNode(root_path="models", name="flower_cnn")
17+
# cnn_node.build(n_epochs=10, train_dir="data/Flower_MNIST", use_file_names=True)
18+
# clear
19+
# X = Image.open("data/Flower_MNIST/rose/0006.jpg")
20+
# X = np.array(X)
21+
img = np.resize(img0, (64, 64, 3))
22+
img = np.reshape(img, (img.shape[0], img.shape[1], 3))
23+
print(X.shape)
24+
cnn_node.predict(img, channels=3,show_fig=True)

examples-graph/test6_evaluate.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from mmkfeatures.graph.gan_node import GanNode
2+
from mmkfeatures.graph.cnn_node import CnnNode
3+
from PIL import Image
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
if __name__ == "__main__":
8+
label="calendula"
9+
root_path="models/gan_flower_calendula"
10+
data_path=f"data/Flower_MNIST/{label}/0003.jpg"
11+
gnode=GanNode(root_path=root_path, dim=64)
12+
# gnode.create_samples_from(src_img_path=data_path)
13+
# gnode.build_model(iterations=1000)
14+
# gnode.export_performance()
15+
X=gnode.generate(n_samples=10)
16+
17+
cnn_node = CnnNode(root_path="models", name="flower_cnn")
18+
# cnn_node.build(n_epochs=10, train_dir="data/Flower_MNIST", use_file_names=True)
19+
list_result=[]
20+
print("len(X) = ",len(X))
21+
num_correct=0
22+
num_total=len(X)
23+
for img in X:
24+
# X = Image.open("data/Flower_MNIST/rose/0006.jpg")
25+
# X = np.array(X)
26+
# img = np.resize(img, (64, 64, 3))
27+
# img = np.reshape(img, (img.shape[0], img.shape[1], 3))
28+
predicted=cnn_node.predict(img, channels=3,show_fig=False)
29+
if label==predicted:
30+
num_correct+=1
31+
32+
list_result.append(predicted)
33+
34+
# results
35+
print("the results: ")
36+
for x in list_result:
37+
print(x)
38+
print("correct rate: ",round(num_correct*1.0/num_total,4))
39+

0 commit comments

Comments
 (0)