forked from mindspore-Ecosystem/mindspore
!7111 Alexnet network define update.
Merge pull request !7111 from linqingke/mass
This commit is contained in:
commit
8bd60d1e3c
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue