Skip to content

Commit

Permalink
fix check path; fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wz authored and wz committed Nov 27, 2020
1 parent 0133ce6 commit 52dac0a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 23 deletions.
17 changes: 10 additions & 7 deletions pytorch_classification/ConfusionMatrix/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import json

import torch
from torchvision import transforms, datasets
import json
import os
from model import MobileNetV2
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from prettytable import PrettyTable

from model import MobileNetV2


class ConfusionMatrix(object):
"""
Expand Down Expand Up @@ -39,9 +42,9 @@ def summary(self):
FP = np.sum(self.matrix[i, :]) - TP
FN = np.sum(self.matrix[:, i]) - TP
TN = np.sum(self.matrix) - TP - FP - FN
Precision = round(TP / (TP + FP), 3)
Recall = round(TP / (TP + FN), 3)
Specificity = round(TN / (TN + FP), 3)
Precision = round(TP / (TP + FP), 3) if TP + FP != 0 else 0.
Recall = round(TP / (TP + FN), 3) if TP + FN != 0 else 0.
Specificity = round(TN / (TN + FP), 3) if TN + FP != 0 else 0.
table.add_row([self.labels[i], Precision, Recall, Specificity])
print(table)

Expand Down Expand Up @@ -111,7 +114,7 @@ def plot(self):
confusion = ConfusionMatrix(num_classes=5, labels=labels)
net.eval()
with torch.no_grad():
for val_data in validate_loader:
for val_data in tqdm(validate_loader):
val_images, val_labels = val_data
outputs = net(val_images.to(device))
outputs = torch.softmax(outputs, dim=1)
Expand Down
24 changes: 14 additions & 10 deletions tensorflow_classification/ConfusionMatrix/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import os
import math
import json
import glob

from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
from model import MobileNetV2
import tensorflow as tf
import json
import os
import math
import numpy as np
from tqdm import tqdm
from prettytable import PrettyTable

from model import MobileNetV2


class ConfusionMatrix(object):
"""
Expand Down Expand Up @@ -40,9 +44,9 @@ def summary(self):
FP = np.sum(self.matrix[i, :]) - TP
FN = np.sum(self.matrix[:, i]) - TP
TN = np.sum(self.matrix) - TP - FP - FN
Precision = round(TP / (TP + FP), 3)
Recall = round(TP / (TP + FN), 3)
Specificity = round(TN / (TN + FP), 3)
Precision = round(TP / (TP + FP), 3) if TP + FP != 0 else 0.
Recall = round(TP / (TP + FN), 3) if TP + FN != 0 else 0.
Specificity = round(TN / (TN + FP), 3) if TN + FP != 0 else 0.
table.add_row([self.labels[i], Precision, Recall, Specificity])
print(table)

Expand Down Expand Up @@ -107,8 +111,8 @@ def pre_function(img):

model = MobileNetV2(num_classes=5)
# feature.build((None, 224, 224, 3)) # when using subclass model
pre_weights_path = './MobileNetV2.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
pre_weights_path = './myMobileNet.ckpt'
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
model.load_weights(pre_weights_path)

# read class_indict
Expand All @@ -121,7 +125,7 @@ def pre_function(img):
confusion = ConfusionMatrix(num_classes=5, labels=labels)

# validate
for step in range(math.ceil(total_val / batch_size)):
for step in tqdm(range(math.ceil(total_val / batch_size))):
val_images, val_labels = next(val_data_gen)
results = model.predict_on_batch(val_images)
results = tf.keras.layers.Softmax()(results).numpy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
import json
import os
import glob
from tensorflow.keras import layers, models, Model, Sequential


Expand Down Expand Up @@ -94,7 +95,7 @@ def pre_function(img: np.ndarray):
model = AlexNet_pytorch(im_height=im_height, im_width=im_width, class_num=5)

pre_weights_path = './pretrain_weights.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
model.load_weights(pre_weights_path)
for layer_t in model.layers:
if 'conv2d' in layer_t.name:
Expand Down
3 changes: 2 additions & 1 deletion tensorflow_classification/Test3_vgg/fine_train_vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
import json
import os
import glob


def main():
Expand Down Expand Up @@ -68,7 +69,7 @@ def pre_function(img):
model = vgg("vgg16", 224, 224, 5)

pre_weights_path = './pretrain_weights.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
model.load_weights(pre_weights_path)
for layer_t in model.layers:
if layer_t.name == 'feature':
Expand Down
3 changes: 2 additions & 1 deletion tensorflow_classification/Test4_goolenet/train_add_bn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
import json
import os
import glob
import numpy as np


Expand Down Expand Up @@ -68,7 +69,7 @@ def pre_function(img: np.ndarray):
# model.build((batch_size, 224, 224, 3)) # when using subclass model

pre_weights_path = './pretrain_weights.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
model.load_weights(pre_weights_path)
model.summary()

Expand Down
3 changes: 2 additions & 1 deletion tensorflow_classification/Test5_resnet/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
import json
import os
import glob
import PIL.Image as im
import numpy as np

Expand Down Expand Up @@ -69,7 +70,7 @@ def pre_function(img):
# feature.build((None, 224, 224, 3)) # when using subclass model

pre_weights_path = './pretrain_weights.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
feature.load_weights(pre_weights_path)
feature.trainable = False
feature.summary()
Expand Down
1 change: 0 additions & 1 deletion tensorflow_classification/Test5_resnet/trainGPU.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def main():
print("using {} images for training, {} images for validation.".format(train_num,
val_num))


def process_train_img(img_path, label):
label = tf.one_hot(label, depth=class_num)
image = tf.io.read_file(img_path)
Expand Down
3 changes: 2 additions & 1 deletion tensorflow_classification/Test6_mobilenet/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tensorflow as tf
import json
import os
import glob
import PIL.Image as im
import numpy as np

Expand Down Expand Up @@ -65,7 +66,7 @@ def pre_function(img):
# feature.build((None, 224, 224, 3)) # when using subclass model

pre_weights_path = './pretrain_weights.ckpt'
assert os.path.exists(pre_weights_path), "cannot find {}".format(pre_weights_path)
assert len(glob.glob(pre_weights_path+"*")), "cannot find {}".format(pre_weights_path)
model.load_weights(pre_weights_path)
for layer_t in model.layers[:-1]:
layer_t.trainable = False
Expand Down

0 comments on commit 52dac0a

Please sign in to comment.