# 除了最后的卷积层外,进一步增大了输出通道数 # 前两个卷积层后部使用池化层来减小输入的高和宽 nn.Conv2D(384, kernel_size=3, padding=1, activation='relu'), nn.Conv2D(384, kernel_size=3, padding=1, activation='relu'), nn.Conv2D(256, kernel_size=3, padding=1, activation='relu'), nn.MaxPool2D(pool_size=3, strides=2), # 这里全连接层的输出个数比LeNet中大数倍,使用丢弃层来缓解过拟合 nn.Dense(4096, activation='relu'), nn.Dropout(0.5), nn.Dense(4096, activation='relu'), nn.Dropout(0.5), # 输出层,对应Fashion-MNIST数据集的10种类别 nn.Dense(10)) X = nd.random.uniform(shape=(1, 1, 224, 224)) net.initialize() for layer in net: X = layer(X) print(layer.name, 'output shape:\t', X.shape) batch_size = 128 train_iter, test_iter = util.load_data_fashion_mnist(batch_size, resize=224) learning_rate = 0.01 num_epochs = 5 ctx = util.try_gpu() net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier()) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': learning_rate}) util.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)
ratios = [[1, 2, 0.5]] * 5 num_anchors = len(sizes[0]) + len(ratios[0]) - 1 # 创建模型 net = TinySSD(num_classes=1) net.initialize() X = nd.zeros((32.3, 256, 256)) anchors, cls_preds, bbox_preds = net(X) print('output anchors:', anchors.shape) print('output class preds:', cls_preds.shape) print('output bbox preds:', bbox_preds.shape) # 读物数据集,初始化 batch_size = 32 train_iter, _ = util.load_data_pikachu(batch_size) ctx, net = util.try_gpu(), TinySSD(num_classes=1) net.initialize(init=init.Xavier(), ctx=ctx) trainer = gluon.Trainer(net.collect_params(), 'sgd', { 'learning_rate': 0.2, 'wd': 5e-4 }) # 定义损失函数和评价函数 cls_loss = gluon.loss.SoftmaxCrossEntropyLoss() bbox_loss = gluon.loss.L1Loss() # 训练模型 for epoch in range(20): acc_sum, mae_sum, n, m = 0.0, 0.0, 0, 0 train_iter.reset() # 从头读取数据 start = time.time()
if __name__ == '__main__': block = Residual(3) block.initialize() X = nd.random.uniform(shape=(4, 3, 6, 6)) print(block(X).shape) block = Residual(6, use_1x1conv=True, strides=2) block.initialize() print(block(X).shape) net = nn.Sequential() net.add(nn.Conv2D(64, kernel_size=7, padding=3, strides=2), nn.BatchNorm(), nn.Activation('relu'), nn.MaxPool2D(pool_size=3, padding=1, strides=2)) net.add(resnet_block(64, 2, first_block=True), resnet_block(128, 2), resnet_block(256, 2), resnet_block(512, 2)) net.add(nn.GlobalAvgPool2D(), nn.Dense(10)) X = nd.random.uniform(shape=(1, 1, 224, 224)) net.initialize() for layer in net: X = layer(X) print(layer.name, 'output shape:\t', X.shape) lr, num_epochs, batch_size, ctx = 0.05, 5, 256, util.try_gpu() net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier()) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) train_iter, test_iter = util.load_data_fashion_mnist(batch_size, resize=96) util.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)
nn.MaxPool2D(pool_size=3, strides=2, padding=1)) b3 = nn.Sequential() b3.add(Inception(64, (96, 128), (16, 32), 32), Inception(128, (128, 192), (32, 96), 64), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) b4 = nn.Sequential() b4.add(Inception(192, (96, 208), (16, 48), 64), Inception(160, (112, 224), (24, 64), 64), Inception(128, (128, 256), (24, 64), 64), Inception(112, (144, 288), (32, 64), 64), Inception(256, (160, 320), (32, 128), 128), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) b5 = nn.Sequential() b5.add(Inception(256, (160, 320), (32, 128), 128), Inception(384, (192, 384), (48, 128), 128), nn.GlobalAvgPool2D()) net = nn.Sequential() net.add(b1, b2, b3, b4, b5, nn.Dense(10)) X = nd.random.uniform(shape=(1, 1, 96, 96)) net.initialize() for layer in net: X = layer(X) print(layer.name, 'output shape:\t', X.shape) lr, num_epochs, batch_size, ctx = 0.1, 5, 128, util.try_gpu() net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier()) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) train_iter, test_iter = util.load_data_fashion_mnist(batch_size, resize=96) util.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)