コード例 #1
0
    def __init__(self,
                 img_rows=224,
                 img_cols=224,
                 weights="imagenet",
                 classes=1000,
                 **kwargs):
        super(ResNet50, self).__init__(use_queue=False, **kwargs)
        self.img_cols = img_cols
        self.img_rows = img_rows
        self.weights = weights
        self.classes = classes

        input = Feature(shape=(None, self.img_rows, self.img_cols, 3))
        labels = Label(shape=(None, self.classes))

        conv1 = Conv2D(num_outputs=64,
                       kernel_size=7,
                       stride=2,
                       activation='linear',
                       padding='same',
                       in_layers=[input])
        bn1 = BatchNorm(in_layers=[conv1])
        ac1 = ReLU(bn1)
        pool1 = MaxPool2D(ksize=[1, 3, 3, 1], in_layers=[bn1])

        cb1 = self.conv_block(pool1, 3, [64, 64, 256], 1)
        id1 = self.identity_block(cb1, 3, [64, 64, 256])
        id1 = self.identity_block(id1, 3, [64, 64, 256])

        cb2 = self.conv_block(id1, 3, [128, 128, 512])
        id2 = self.identity_block(cb2, 3, [128, 128, 512])
        id2 = self.identity_block(id2, 3, [128, 128, 512])
        id2 = self.identity_block(id2, 3, [128, 128, 512])

        cb3 = self.conv_block(id2, 3, [256, 256, 1024])
        id3 = self.identity_block(cb3, 3, [256, 256, 1024])
        id3 = self.identity_block(id3, 3, [256, 256, 1024])
        id3 = self.identity_block(id3, 3, [256, 256, 1024])
        id3 = self.identity_block(cb3, 3, [256, 256, 1024])
        id3 = self.identity_block(id3, 3, [256, 256, 1024])

        cb4 = self.conv_block(id3, 3, [512, 512, 2048])
        id4 = self.identity_block(cb4, 3, [512, 512, 2048])
        id4 = self.identity_block(id4, 3, [512, 512, 2048])

        pool2 = AvgPool2D(ksize=[1, 7, 7, 1], in_layers=[id4])

        flatten = Flatten(in_layers=[pool2])
        dense = Dense(classes, in_layers=[flatten])

        loss = SoftMaxCrossEntropy(in_layers=[labels, dense])
        loss = ReduceMean(in_layers=[loss])
        self.set_loss(loss)
        self.add_output(dense)
コード例 #2
0
 def test_avgpool2D(self):
     """Test that AvgPool2D can be invoked."""
     length = 4
     width = 4
     in_channels = 5
     batch_size = 10
     in_tensor = np.random.rand(batch_size, length, width, in_channels)
     with self.session() as sess:
         in_tensor = tf.convert_to_tensor(in_tensor, dtype=tf.float32)
         out_tensor = AvgPool2D()(in_tensor)
         sess.run(tf.global_variables_initializer())
         out_tensor = out_tensor.eval()
         assert out_tensor.shape == (batch_size, 2, 2, in_channels)