Data Science Asked on October 24, 2020
I’m following a tutorial on tensorflow using a convolutional neural network for images, but I’m looking to do it with grayscale images. How would the code posted there be different if it was for grayscale images instead of colored images with 3 channels?
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
The following will be the updated code for grayscale images:
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
Why?
Because for an RGB image, there are 3 channels, 'R', 'G' and 'B'
So, the input shape will be (height, width, 3)
But since grayscale has only one channel, the input shape becomes (height, width, 1)
Note that if you are using Keras with Tensorflow backend, then the data_format is channels_last
, which means that the input shape should be (height, width, channels)
.
Otherwise, if you are using Theano as the backend, then the input shape should be (channels, height, width)
since Theano uses the channels_first
data format.
Hope this is of help. Please upvote if helpful.
Answered by Fortfanop on October 24, 2020
input_shape=(32, 32, 3)))
will become
input_shape=(32, 32, 1)))
Channel is the last argument by default
"...When using this layer as the first layer in a model, provide the keyword argument input_shape (tuple of integers, does not include the sample axis), e.g. input_shape=(128, 128, 3) for 128x128 RGB pictures in data_format="channels_last..."
Answered by 10xAI on October 24, 2020
1 Asked on December 3, 2020 by yathislax
accuracy data science model machine learning model predictive modeling
1 Asked on December 3, 2020 by m8tey7
1 Asked on December 3, 2020 by user75228
bigdata data cleaning data mining machine learning preprocessing
0 Asked on December 3, 2020 by goodcow
3 Asked on December 3, 2020 by peyman
0 Asked on December 3, 2020 by backlog-giash
data mining feature engineering machine learning scikit learn
1 Asked on December 3, 2020 by bob2
3 Asked on December 3, 2020 by lonsomehell
3 Asked on December 3, 2020 by carl-molnar
2 Asked on December 3, 2020 by new-developer
1 Asked on December 3, 2020 by amar-parajuli
1 Asked on December 3, 2020 by user100780
2 Asked on December 2, 2020 by dhaval-thakkar
2 Asked on December 2, 2020 by david-marques
1 Asked on December 2, 2020 by lame_coder
linear regression machine learning matplotlib python visualization
1 Asked on December 2, 2020 by qbit
1 Asked on December 2, 2020 by antonyellow
1 Asked on December 2, 2020
1 Asked on December 2, 2020 by ken-wang
class imbalance evaluation metric multiclass classification sampling
0 Asked on December 2, 2020 by franva
Get help from others!
Recent Answers
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP