0

I tried the code below with MLP now I need to replace this MLP code with CNN 8 layers with the following structure: 3×3×32 Convolutional → 3×3×64 Convolutional → 2×2 MaxPool → Dropout → Flatten → 1 × 128 Full connected → Dropout → 128 × 10 Fully connected → Softmax.

//declear path to your mnist data folder
img_path = 'c:/kaggleMNISTdata/trainingSet/trainingSet'
 //get the path list using the path object
image_paths = list(paths.list_images(img_path))
//apply our function
image_list, label_list = load(image_paths, verbose=10000)
// binarize the labels
lb = LabelBinarizer()
label_list = lb.fit_transform(label_list)
// split data into training and test set
X_train, X_test, y_train, y_test = train_test_split(image_list,
                                                    label_list,
                                                    test_size=0.1,
                                                    random_state=42)
data = list(zip(image_list, label_list))
random.shuffle(data)
class SimpleMLP:
    @staticmethod
    def build(shape, classes):
        model = Sequential()
        model.add(Dense(200, input_shape=(shape,)))
        model.add(Activation("relu"))
        model.add(Dense(200))
        model.add(Activation("relu"))
        model.add(Dense(classes))
        model.add(Activation("softmax"))
        return model

I tried following

def build(shape, classes):
        model = Sequential()
        model.add(Conv2D(200, input_shape=(shape,)))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Flatten(1))
        model.add(Dropout(1))
        model.add(Dense(200))
        model.add(Activation("softmax"))
        return model

is it correct?

E_net4
  • 27,810
  • 13
  • 101
  • 139
user12
  • 761
  • 8
  • 24
  • 1
    Concerning your structure: Are you sure that you have to use two Convolutional Layers without an intermediary Max-Pooling Layer in the beginning of your network? And which activation functions do you want to use in your Convolutional and Fully-connected layers? – rftr Nov 06 '20 at 06:37
  • yeah the structure should be like that i will use softmax activation function. coud you help – user12 Nov 06 '20 at 13:53
  • See my answer .. – rftr Nov 06 '20 at 14:27

1 Answers1

1

Try this with your given architecture:

cnn_model = Sequential()
cnn_model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
cnn_model.add(Conv2D(64, (3, 3)))
cnn_model.add(MaxPooling2D((2, 2)))
cnn_model.add(Dropout(0.5))
cnn_model.add(Flatten())
cnn_model.add(Dense(128))
cnn_model.add(Dropout(0.5))
cnn_model.add(Dense(classes))
cnn_model.add(Activation("softmax"))
rftr
  • 1,185
  • 2
  • 10
  • 19
  • i am following this tutorial https://ichi.pro/fr/apprentissage-federe-une-mise-en-oeuvre-etape-par-etape-dans-tensorflow-187764832744345 it gives error – user12 Nov 06 '20 at 14:59
  • FFile ".\Updatedcnn.py", line 894, in build global_model = smlp_global.build(784, 10) File ".\Updatedcnn.py", line 894, in build model.add(Conv2D(32, (3, 3), input_shape=(shape,))) File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper result = method(self, *args, **kwargs) – user12 Nov 06 '20 at 14:59
  • File "C:\Users\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\input_spec.py", line 191, in assert_input_compatibility raise ValueError('Input ' + str(input_index) + ' of layer ' + ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 784] – user12 Nov 06 '20 at 15:00
  • 1
    How do you call the `build` - method, especially what is your `shape` parameter? – rftr Nov 09 '20 at 05:47
  • shape parameter is 28*28*1 = 784,& the no of classes is 10. global_model = smlp_global.build(784, 10) , local_model = smlp_local.build(784, 10) – user12 Nov 09 '20 at 21:20
  • 1
    Okay, so we have two models: `global` and `local`. For your global model (first layer is Dense) you can use your static build method with `input_shape=(784,)`. And for your local ones see my edited answer. – rftr Nov 10 '20 at 06:15
  • i didnt understand this "For your global model (first layer is Dense) you can use your static build method with input_shape=(784,)" – user12 Nov 10 '20 at 16:45
  • I was addressing your tutorial where you define an instance from class `SimpleMLP` called `smlp_global`. This model has a Dense layer as first layer and you can use `input_shape=(784,)` here. But for your derived CNN model that you called `local_model` you have to use `input_shape=(28, 28, 1)` since it has a Convolutional layer as first layer. If you want to have further information on layers you can also have a look at [here](https://stackoverflow.com/questions/44747343/keras-input-explanation-input-shape-units-batch-size-dim-etc). – rftr Nov 11 '20 at 05:39