ahmedfgad / numpycnn Goto Github PK
View Code? Open in Web Editor NEWBuilding Convolutional Neural Networks From Scratch using NumPy
Home Page: https://pygad.readthedocs.io
Building Convolutional Neural Networks From Scratch using NumPy
Home Page: https://pygad.readthedocs.io
Hi Ahmed,
if len(img.shape) > 2 or len(conv_filter.shape) > 3: # Check if number of image channels matches the filter depth.
if img.shape[-1] != conv_filter.shape[-1]:
print("Error: Number of channels in both image and filter must match.")
sys.exit()
The above snippet in the conv function checks for channel mismatch. But, if len(img.shape)
is greater than 2 (eg - (224, 224, 3)), and the conv_filter's shape is (2,3,3)
, then the condition if img.shape[-1] != conv_filter.shape[-1]
is False
, even though the number of channels of the image, and the conv_filter, are not the same.
One possible solution is -
if len(img.shape) != len(conv_filter.shape) - 1: # Check whether number of dimensions is the same
print("Error: Number of dimensions in conv filter and image do not match.")
exit()
if len(img.shape) > 2 or len(conv_filter.shape) > 3: # Check if number of image channels matches the filter depth.
if img.shape[-1] != conv_filter.shape[-1]:
print("Error: Number of channels in both image and filter must match.")
sys.exit()
The size of input image is (512,512), and the size of conv1_out is (510,510), the size of pool_out should be (255,255). However the size of pool_out is (254,254). So I suggest that the codes in pooling() function should be changed as following:
pool_out = np.zeros((np.uint16((feature_map.shape[0]-size)/stride+1),
np.uint16((feature_map.shape[1]-size)/stride+1),
feature_map.shape[-1]))
The current implementation of the sigmoid() function can get a RuntimeWarning overflow in cnn.py at line 29 numpy.exp():
"return 1.0 / (1 + numpy.exp(-1 * sop))"
To avoid/correct the overflow, use:
"return numpy.exp(-numpy.logaddexp(0, -sop))"
就是在第二层和第三层的卷积核为什么要用随机数赋值,不是很懂这些原理
your code is wonderful!
but I have one question: how to create my own dataset use code. And how to label or training it?
thank you !
When i run this code have a error that is "operands could not be broadcast together with shapes (7,6) (7,7) "
The current relu function actually does no work for the feature_map.
The code relu_out[r, c, map_num] = numpy.max(feature_map[r, c, map_num], 0)
should be written as
relu_out[r, c, map_num] = numpy.max([feature_map[r, c, map_num], 0])
And thanks for the code you shared!
in the pooling()
change the "feature_map.shape[0]-size+1)/stride" to "feature_map.shape[0]-size)/stride+1"
Lines 85 to 87 in aac7afd
pool_out = numpy.zeros((numpy.uint16((feature_map.shape[0]-size)/stride+1),
numpy.uint16((feature_map.shape[1]-size+1)/stride+1),
feature_map.shape[-1]))
in the relu()
change the "feature_map.shape[1]-size-1" to "feature_map.shape[1]-size+1" to arrive at the end.
Line 90 in aac7afd
for r in numpy.arange(0,feature_map.shape[0]-size+1, stride):
Line 92 in aac7afd
for c in numpy.arange(0, feature_map.shape[1]-size+1, stride):
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.