def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ filter_size = 3 pool_size = 4 self.conv1 = ConvolutionalLayer(input_shape[2], conv1_channels, filter_size, padding=1) self.relu1 = ReLULayer() self.max_pool1 = MaxPoolingLayer(pool_size, stride=pool_size) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding=1) self.relu2 = ReLULayer() self.max_pool2 = MaxPoolingLayer(pool_size, stride=pool_size) self.flatten = Flattener() self.fc = FullyConnectedLayer(n_input=4 * conv2_channels, n_output=n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers input_width, input_height, input_channels = input_shape self.conv1 = ConvolutionalLayer(input_channels, conv1_channels, filter_size=3, padding=1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(pool_size=4, stride=4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size=3, padding=1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(pool_size=4, stride=4) self.flattener = Flattener() self.fc = FullyConnectedLayer( input_width * input_height * conv2_channels // (4**4), n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ width, height, channels = input_shape filter_size = 3 padding = 1 pool_size = 4 pool_stride = 4 self.Conv1 = ConvolutionalLayer(channels, conv1_channels, filter_size, padding) self.ReLU1 = ReLULayer() self.MaxPool1 = MaxPoolingLayer(pool_size, pool_stride) self.Conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding) self.ReLU2 = ReLULayer() self.MaxPool2 = MaxPoolingLayer(pool_size, pool_stride) left_width = width // pool_stride // pool_stride left_height = height // pool_stride // pool_stride self.Flat = Flattener() self.FullyConnected = FullyConnectedLayer( left_width * left_height * conv2_channels, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.height = input_shape[0] self.width = input_shape[1] self.input_channels = input_shape[2] self.n_output_classes = n_output_classes self.conv1_channels = conv1_channels self.conv2_channels = conv2_channels self.conv1_layer = ConvolutionalLayer(self.input_channels, self.conv1_channels, 3, 1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(4, 4) self.conv2_layer = ConvolutionalLayer(self.conv1_channels, self.conv2_channels, 3, 1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(4, 4) self.flattener = Flattener() self.fc_layer = FullyConnectedLayer(2*2*self.conv2_channels, self.n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers self.out_classes = n_output_classes image_width, image_height, in_channels = input_shape self.Conv1 = ConvolutionalLayer(in_channels, conv1_channels, 3, 1) self.ReLU1 = ReLULayer() self.MaxPool1 = MaxPoolingLayer(4, 4) self.Conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.ReLU2 = ReLULayer() self.MaxPool2 = MaxPoolingLayer(4, 4) self.Flatten = Flattener() self.FC = FullyConnectedLayer(4 * conv2_channels, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers self.input_shape = input_shape self.n_output_classes = n_output_classes self.layer1 = ConvolutionalLayer(3, conv1_channels, 3, 1) #32x32x3xconv1_channels self.layer2 = ReLULayer() self.layer3 = MaxPoolingLayer(4, 4) #8x8x3xconv1_channels self.layer4 = ConvolutionalLayer( conv1_channels, conv2_channels, 3, 1) #8x8x3x conv1_channels x conv2_channels self.layer5 = ReLULayer() self.layer6 = MaxPoolingLayer( 4, 4) #2x2x3 conv1_channels x conv2_channels self.layer7 = Flattener() self.layer8 = FullyConnectedLayer(conv1_channels * conv2_channels * 2, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ width, height, channels = input_shape self.conv1 = ConvolutionalLayer(in_channels=3, out_channels=3, filter_size=conv1_channels) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(in_channels=3, out_channels=3, filter_size=conv2_channels) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(4, 4) self.flatten = Flattener() self.f_connected = FullyConnectedLayer(3, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO_ Create necessary layers # raise Exception("Not implemented!") weight, height, cannels = input_shape filter_size = 3 pool_size = 4 padding = 1 stride = pool_size self.conv1 = ConvolutionalLayer(cannels, conv1_channels, filter_size, padding) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(pool_size, stride) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(pool_size, stride) self.flatten = Flattener() n_fc_input = int(height / pool_size / pool_size * weight / pool_size / pool_size * conv2_channels) self.fc = FullyConnectedLayer(n_fc_input, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers width, height, n_channels = input_shape self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, 3, 1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(4, 4) self.flatten = Flattener() self.fc = FullyConnectedLayer( (height // 4 // 4) * (width // 4 // 4) * conv2_channels, n_output_classes) self.conv1_params = self.conv1.params() self.conv2_params = self.conv2.params() self.fc_params = self.fc.params()
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers #raise Exception("Not implemented!") image_width, image_height, n_channels = input_shape self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, 3, 1) self.relu1 = ReLULayer() self.maxp1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.relu2 = ReLULayer() self.maxp2 = MaxPoolingLayer(4, 4) self.flatn = Flattener() fc_input = int(image_width * image_height * conv2_channels / pow(4, 4)) self.fc = FullyConnectedLayer(fc_input, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers self.layers = [ ConvolutionalLayer(input_shape[2], conv1_channels, 3, 0), ReLULayer(), MaxPoolingLayer(4, 4), ConvolutionalLayer(conv1_channels, conv2_channels, 3, 0), ReLULayer(), MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer( int(input_shape[0] * input_shape[1] * conv2_channels / 256), n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers # raise Exception("Not implemented!") self.out_classes = n_output_classes image_width, image_height, in_channels = input_shape self.layers = [ ConvolutionalLayer(in_channels, conv1_channels, 3, 1), ReLULayer(), MaxPoolingLayer(4, 4), ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1), ReLULayer(), MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer(4 * conv2_channels, n_output_classes) ]
def __init__(self, n_input, n_output, conv1_size, conv2_size, reg): """ Initializes the neural network Arguments: n_input, int - dimension of the model input n_output, int - number of classes to predict conv1_size, int - number of filters in the 1st conv layer conv2_size, int - number of filters in the 2nd conv layer reg, float - L2 regularization strength """ self.reg = reg height, width, input_channels = n_input self.L = [ ConvolutionalLayer(in_channels=input_channels, out_channels=conv1_size, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(4, 4), ConvolutionalLayer(in_channels=conv1_size, out_channels=conv2_size, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(4, 4), Flattener(), FullyConnectedLayer(8, n_output) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.layers = [ ConvolutionalLayer(in_channels=input_shape[2], out_channels=input_shape[2], filter_size=conv1_channels, padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), #2 ConvolutionalLayer(in_channels=input_shape[2], out_channels=input_shape[2], filter_size=conv2_channels, padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), #2 Flattener(), FullyConnectedLayer(n_input=192, n_output=n_output_classes) ] #192
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.model = [ ConvolutionalLayer(in_channels=input_shape[2], out_channels=conv1_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(2, 2), ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(2, 2), Flattener(), FullyConnectedLayer(n_input=int(input_shape[0] / 4)**2 * conv2_channels, n_output=n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ width, height, n_channels = input_shape conv = 3 pad = 1 stride = 4 pool = 4 hidden_layer_size = (width // stride // stride) * (height // stride // stride) * conv2_channels self.layers = [ ConvolutionalLayer(n_channels, conv1_channels, conv, pad), ReLULayer(), MaxPoolingLayer(pool, stride), ConvolutionalLayer(conv1_channels, conv2_channels, conv, pad), ReLULayer(), MaxPoolingLayer(pool, stride), Flattener(), FullyConnectedLayer(hidden_layer_size, n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels, filter_size=3): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.input_shape = input_shape self.n_output_classes = n_output_classes self.conv1_channels = conv1_channels self.conv2_channels = conv2_channels self.filter_size = filter_size self.padding = 1 c1 = int( (input_shape[0] - self.filter_size + 2 * self.padding) / 1) + 1 mp1 = int((c1 - 4) / 4) + 1 c2 = int((mp1 - self.filter_size + 2 * self.padding) / 1) + 1 self.size_after_2maxpool = int((c2 - 4) / 4) + 1 self.RL1 = ReLULayer() self.RL2 = ReLULayer() self.MaxPool1 = MaxPoolingLayer(pool_size=4, stride=4) self.MaxPool2 = MaxPoolingLayer(pool_size=4, stride=4) self.Flatten = Flattener() self.Conv1 = ConvolutionalLayer(in_channels=self.input_shape[-1], out_channels=conv1_channels, filter_size=self.filter_size, padding=self.padding) self.Conv2 = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=self.filter_size, padding=self.padding) self.FC = FullyConnectedLayer(n_input=conv2_channels * self.size_after_2maxpool**2, n_output=self.n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.conv1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.reLu1 = ReLULayer() self.mxPl1 = MaxPoolingLayer(4, 4) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.reLu2 = ReLULayer() self.mxPl2 = MaxPoolingLayer(4, 4) self.flat = Flattener() self.fCL = FullyConnectedLayer(4 * conv2_channels, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels, reg): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.reg = reg self.conv1 = ConvolutionalLayer(in_channels=input_shape[-1], out_channels=conv1_channels, filter_size=3, padding=1) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(pool_size=4, stride=4) self.conv2 = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(pool_size=4, stride=4) self.flattener = Flattener() ## n_input = 4*conv2_channels - hard coding here, because of constant picture size 32 32 3 self.fullyconlayer = FullyConnectedLayer(n_input=4 * conv2_channels, n_output=n_output_classes) self.W_fc_layer = None self.B_fc_layer = None self.W_con1_layer = None self.B_con1_layer = None self.W_con2_layer = None self.B_con2_layer = None
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network :param input_shape: tuple of 3 ints - image_width, image_height, n_channels :param n_output_classes: int - number of classes to predict :param conv1_channels: int - number of filters in the 1st conv layer :param conv2_channels: int - number of filters in the 2nd conv layer """ self.convolution_one = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.relu_one = ReLULayer() self.maxpool_one = MaxPoolingLayer(4, 4) self.convolution_two = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.relu_two = ReLULayer() self.maxpool_two = MaxPoolingLayer(4, 4) self.flattener = Flattener() height = ((input_shape[0] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4 width = ((input_shape[1] + 2 * 1 - 3 + 1) // 4 + 2 * 1 - 3 + 1) // 4 self.fc = FullyConnectedLayer(width * height * conv2_channels, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ self.layer_1 = ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1) self.layer_2 = ReLULayer() self.layer_3 = MaxPoolingLayer(4, 4) self.layer_4 = ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1) self.layer_5 = ReLULayer() self.layer_6 = MaxPoolingLayer(4, 4) self.layer_7 = Flattener() self.layer_8 = FullyConnectedLayer( (input_shape[0] * input_shape[1] * conv2_channels) // (16**2), n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ filter_size = 3 padding = 1 pool_size = 4 stride = 4 width, height, n_channels = input_shape assert ((height + 2 * padding - filter_size + 1) % pool_size == 0) assert ((width + 2 * padding - filter_size + 1) % pool_size == 0) height = (height + 2 * padding - filter_size + 1) // pool_size width = (width + 2 * padding - filter_size + 1) // pool_size assert ((height + 2 * padding - filter_size + 1) % pool_size == 0) assert ((width + 2 * padding - filter_size + 1) % pool_size == 0) height = (height + 2 * padding - filter_size + 1) // pool_size width = (width + 2 * padding - filter_size + 1) // pool_size # TODO Create necessary layers self.Conv_1 = ConvolutionalLayer(n_channels, conv1_channels, filter_size, padding) self.Relu_1 = ReLULayer() self.Maxpool_1 = MaxPoolingLayer(pool_size, stride) self.Conv_2 = ConvolutionalLayer(conv1_channels, conv2_channels, filter_size, padding) self.Relu_2 = ReLULayer() self.Maxpool_2 = MaxPoolingLayer(pool_size, stride) self.Flattener = Flattener() self.FC = FullyConnectedLayer(height * width * conv2_channels, n_output_classes)
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers image_width, image_height, n_channels = input_shape conv_padding = 0 conv_filter_size = 3 max_pool_size = 4 max_pool_stride = 1 conv1_output_size = image_width - conv_filter_size + 1 maxpool1_output_size = int( (conv1_output_size - max_pool_size) / max_pool_stride) + 1 conv2_output_size = maxpool1_output_size - conv_filter_size + 1 maxpool2_output_size = int( (conv2_output_size - max_pool_size) / max_pool_stride) + 1 # correct if height == width !!! fc_input_size = maxpool2_output_size * maxpool2_output_size * conv2_channels self.conv1 = ConvolutionalLayer(n_channels, conv1_channels, conv_filter_size, conv_padding) self.relu1 = ReLULayer() self.maxpool1 = MaxPoolingLayer(max_pool_size, max_pool_stride) self.conv2 = ConvolutionalLayer(conv1_channels, conv2_channels, conv_filter_size, conv_padding) self.relu2 = ReLULayer() self.maxpool2 = MaxPoolingLayer(max_pool_size, max_pool_stride) self.flattener = Flattener() self.fc = FullyConnectedLayer(fc_input_size, n_output_classes)
def _initialize_weights(self): flattener = Flattener() flattened = flattener(self) for m in flattened: if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): n = m.weight.size(1) m.weight.data.normal_(0, 0.01) m.bias.data.zero_()
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers width, height, im_channels = input_shape out_height_conv1 = int((height - 3 + 2 * 1) / 1 + 1) out_width_conv1 = int((width - 3 + 2 * 1) / 1 + 1) out_height_maxpool1 = int((out_height_conv1 - 4) / 2 + 1) out_width_maxpool1 = int((out_width_conv1 - 4) / 2 + 1) out_height_conv2 = int((out_height_maxpool1 - 3 + 2 * 1) / 1 + 1) out_width_conv2 = int((out_width_maxpool1 - 3 + 2 * 1) / 1 + 1) out_height_maxpool2 = int((out_height_conv2 - 4) / 2 + 1) out_width_maxpool2 = int((out_width_conv2 - 4) / 2 + 1) neurons_in_fc = out_height_maxpool2 * out_width_maxpool2 * conv2_channels self.sequential = [ ConvolutionalLayer(im_channels, conv1_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), ConvolutionalLayer(conv1_channels, conv2_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(pool_size=4, stride=2), Flattener(), FullyConnectedLayer(neurons_in_fc, n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers # padding=0, stride=1 self.padding = 0 self.stride = 1 self.filter_size = 3 self.pool_size = 4 self.conv1_out_size = int((input_shape[0] - self.filter_size + 2 * self.padding) / self.stride + 1) self.max_pool1_out_size = int((self.conv1_out_size - self.pool_size) / self.stride + 1) self.conv2_out_size = int((self.max_pool1_out_size - self.filter_size + 2 * self.padding) / self.stride + 1) self.max_pool2_out_size = int((self.conv2_out_size - self.pool_size) / self.stride + 1) # padding=0, stride=1 self.sequence = [ ConvolutionalLayer(input_shape[2], conv1_channels, self.filter_size, self.padding), ReLULayer(), MaxPoolingLayer(self.pool_size, self.stride), ConvolutionalLayer(conv1_channels, conv2_channels, self.filter_size, self.padding), ReLULayer(), MaxPoolingLayer(self.pool_size, self.stride), Flattener(), FullyConnectedLayer( self.max_pool2_out_size * self.max_pool2_out_size * conv2_channels, n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): self.layers = [ConvolutionalLayer(in_channels=input_shape[2], \ out_channels=input_shape[2], \ filter_size=conv1_channels, \ padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, \ stride=2), ConvolutionalLayer(in_channels=input_shape[2], \ out_channels=input_shape[2], \ filter_size=conv2_channels, \ padding=2), ReLULayer(), MaxPoolingLayer(pool_size=4, \ stride=2), Flattener(), FullyConnectedLayer(n_input=192, \ n_output=n_output_classes)]
def __init__(self, input_shape, n_input_classes, n_output_classes, conv1_channels, conv2_channels, reg): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict stride = 1 conv1_channels, int - number of filters in the 1st conv layer, padding=1 after first conv - shape (2, 32, 32, 2) after first max pooling - shape (2, 8, 8, 2) conv2_channels, int - number of filters in the 2nd conv layer, padding=1 after second conv - shape (2, 8, 8, 2) after second max poolling - width =2, height = 2 channels = 2 for convolutional layer in_channels = 8 """ # TODO Create necessary layers layers = [] image_width, image_height, channels = input_shape conv1_layer = ConvolutionalLayer(in_channels=channels, out_channels=conv1_channels, filter_size=3, padding=1) layers.append(conv1_layer) layers.append(ReLULayer()) layers.append(MaxPoolingLayer(4, 4)) conv2_layer = ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1) layers.append(conv2_layer) layers.append(ReLULayer()) layers.append(MaxPoolingLayer(4, 4)) layers.append(Flattener()) layers.append(FullyConnectedLayer(n_input_classes, n_output_classes)) self.layers = layers self.reg = reg
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers #raise Exception("Not implemented!") image_width, image_height, image_channels = input_shape maxpool1_size = 4 maxpool2_size = 4 flattener_width = int(image_width / (maxpool1_size * maxpool2_size)) flattener_height = int(image_width / (maxpool1_size * maxpool2_size)) self.layers = [ ConvolutionalLayer(in_channels=image_channels, out_channels=conv1_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(maxpool1_size, maxpool1_size), ConvolutionalLayer(in_channels=conv1_channels, out_channels=conv2_channels, filter_size=3, padding=1), ReLULayer(), MaxPoolingLayer(maxpool2_size, maxpool2_size), Flattener(), FullyConnectedLayer( flattener_width * flattener_height * conv2_channels, n_output_classes) ]
def __init__(self, input_shape, n_output_classes, conv1_channels, conv2_channels): """ Initializes the neural network Arguments: input_shape, tuple of 3 ints - image_width, image_height, n_channels Will be equal to (32, 32, 3) n_output_classes, int - number of classes to predict conv1_channels, int - number of filters in the 1st conv layer conv2_channels, int - number of filters in the 2nd conv layer """ # TODO Create necessary layers #raise Exception("Not implemented!") self.layers = {"conv1": ConvolutionalLayer(input_shape[2], conv1_channels, 3, 1), "relu1": ReLULayer(), "maxpool1": MaxPoolingLayer(4, 4), "conv2": ConvolutionalLayer(conv1_channels, conv2_channels, 3, 1), "relu2": ReLULayer(), "maxpool2": MaxPoolingLayer(4, 4), "flatten": Flattener(), "fc": FullyConnectedLayer((input_shape[0]//16)*(input_shape[1]//16)*conv2_channels, n_output_classes)}