diff --git a/model_zoo/official/cv/alexnet/eval.py b/model_zoo/official/cv/alexnet/eval.py index 5627e05c2d4..69ce802109a 100644 --- a/model_zoo/official/cv/alexnet/eval.py +++ b/model_zoo/official/cv/alexnet/eval.py @@ -50,7 +50,7 @@ if __name__ == "__main__": if args.dataset_name == 'cifar10': cfg = alexnet_cifar10_cfg - network = AlexNet(cfg.num_classes) + network = AlexNet(cfg.num_classes, phase='test') loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") opt = nn.Momentum(network.trainable_params(), cfg.learning_rate, cfg.momentum) ds_eval = create_dataset_cifar10(args.data_path, cfg.batch_size, status="test", target=args.device_target) @@ -64,7 +64,7 @@ if __name__ == "__main__": elif args.dataset_name == 'imagenet': cfg = alexnet_imagenet_cfg - network = AlexNet(cfg.num_classes) + network = AlexNet(cfg.num_classes, phase='test') loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") ds_eval = create_dataset_imagenet(args.data_path, cfg.batch_size, training=False) diff --git a/model_zoo/official/cv/alexnet/src/alexnet.py b/model_zoo/official/cv/alexnet/src/alexnet.py index 2d558dc5e6e..307edffc7b3 100644 --- a/model_zoo/official/cv/alexnet/src/alexnet.py +++ b/model_zoo/official/cv/alexnet/src/alexnet.py @@ -14,43 +14,38 @@ # ============================================================================ """Alexnet.""" import mindspore.nn as nn -from mindspore.common.initializer import TruncatedNormal from mindspore.ops import operations as P -def conv(in_channels, out_channels, kernel_size, stride=1, padding=0, pad_mode="valid"): - weight = weight_variable() - return nn.Conv2d(in_channels, out_channels, - kernel_size=kernel_size, stride=stride, padding=padding, - weight_init=weight, has_bias=False, pad_mode=pad_mode) - -def fc_with_initialize(input_channels, out_channels): - weight = weight_variable() - bias = weight_variable() - return nn.Dense(input_channels, out_channels, weight, bias) - -def weight_variable(): - return TruncatedNormal(0.02) +def conv(in_channels, out_channels, kernel_size, stride=1, padding=0, pad_mode="valid", has_bias=True): + return nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, + has_bias=has_bias, pad_mode=pad_mode) +def fc_with_initialize(input_channels, out_channels, has_bias=True): + return nn.Dense(input_channels, out_channels, has_bias=has_bias) class AlexNet(nn.Cell): """ Alexnet """ - def __init__(self, num_classes=10, channel=3, include_top=True): + def __init__(self, num_classes=10, channel=3, phase='train', include_top=True): super(AlexNet, self).__init__() - self.conv1 = conv(channel, 96, 11, stride=4) - self.conv2 = conv(96, 256, 5, pad_mode="same") - self.conv3 = conv(256, 384, 3, pad_mode="same") - self.conv4 = conv(384, 384, 3, pad_mode="same") - self.conv5 = conv(384, 256, 3, pad_mode="same") - self.relu = nn.ReLU() - self.max_pool2d = P.MaxPool(ksize=3, strides=2) + self.conv1 = conv(channel, 64, 11, stride=4, pad_mode="same", has_bias=True) + self.conv2 = conv(64, 192, 5, pad_mode="same", has_bias=True) + self.conv3 = conv(192, 384, 3, pad_mode="same", has_bias=True) + self.conv4 = conv(384, 256, 3, pad_mode="same", has_bias=True) + self.conv5 = conv(256, 256, 3, pad_mode="same", has_bias=True) + self.relu = P.ReLU() + self.max_pool2d = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='valid') self.include_top = include_top if self.include_top: + dropout_ratio = 0.65 + if phase == 'test': + dropout_ratio = 1.0 self.flatten = nn.Flatten() self.fc1 = fc_with_initialize(6 * 6 * 256, 4096) self.fc2 = fc_with_initialize(4096, 4096) self.fc3 = fc_with_initialize(4096, num_classes) + self.dropout = nn.Dropout(dropout_ratio) def construct(self, x): """define network""" @@ -72,7 +67,9 @@ class AlexNet(nn.Cell): x = self.flatten(x) x = self.fc1(x) x = self.relu(x) + x = self.dropout(x) x = self.fc2(x) x = self.relu(x) + x = self.dropout(x) x = self.fc3(x) return x diff --git a/model_zoo/official/cv/alexnet/src/config.py b/model_zoo/official/cv/alexnet/src/config.py index 1e566ecaa91..f2e839f0204 100644 --- a/model_zoo/official/cv/alexnet/src/config.py +++ b/model_zoo/official/cv/alexnet/src/config.py @@ -39,8 +39,8 @@ alexnet_imagenet_cfg = edict({ 'epoch_size': 150, 'batch_size': 256, 'buffer_size': None, # invalid parameter - 'image_height': 227, - 'image_width': 227, + 'image_height': 224, + 'image_width': 224, 'save_checkpoint_steps': 625, 'keep_checkpoint_max': 10, 'air_name': "alexnet.air", diff --git a/model_zoo/official/cv/alexnet/train.py b/model_zoo/official/cv/alexnet/train.py index aaaf7fbbf38..67b45e13440 100644 --- a/model_zoo/official/cv/alexnet/train.py +++ b/model_zoo/official/cv/alexnet/train.py @@ -94,7 +94,7 @@ if __name__ == "__main__": if ds_train.get_dataset_size() == 0: raise ValueError("Please check dataset size > 0 and batch_size <= dataset size") - network = AlexNet(cfg.num_classes) + network = AlexNet(cfg.num_classes, phase='train') loss_scale_manager = None metrics = None