Skip to content

Commit

Permalink
AlexNet and VGG Implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
aksrajvanshi authored and stu1130 committed Jul 10, 2020
1 parent 6717af7 commit e2b0f8b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 125 deletions.
108 changes: 46 additions & 62 deletions chapter_convolutional-modern/alexnet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -309,7 +309,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -323,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -332,37 +332,39 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ai.djl.Device;\n",
"import ai.djl.Model;\n",
"import ai.djl.basicdataset.FashionMnist;\n",
"import ai.djl.modality.cv.transform.Resize;\n",
"import ai.djl.modality.cv.transform.ToTensor;\n",
"import ai.djl.metric.Metrics;\n",
"import ai.djl.ndarray.NDArray;\n",
"import ai.djl.ndarray.NDList;\n",
"import ai.djl.ndarray.NDManager;\n",
"import ai.djl.ndarray.types.DataType;\n",
"import ai.djl.ndarray.types.Shape;\n",
"import ai.djl.nn.Activation;\n",
"import ai.djl.nn.Blocks;\n",
"import ai.djl.nn.SequentialBlock;\n",
"import ai.djl.nn.convolutional.Conv2D;\n",
"import ai.djl.nn.convolutional.Conv2d;\n",
"import ai.djl.nn.core.Linear;\n",
"import ai.djl.nn.norm.Dropout;\n",
"import ai.djl.nn.pooling.Pool;\n",
"import ai.djl.training.DefaultTrainingConfig;\n",
"import ai.djl.training.EasyTrain;\n",
"import ai.djl.nn.norm.Dropout;\n",
"import ai.djl.training.Trainer;\n",
"import ai.djl.training.dataset.Dataset;\n",
"import ai.djl.training.dataset.ArrayDataset;\n",
"import ai.djl.training.evaluator.Accuracy;\n",
"import ai.djl.training.listener.TrainingListener;\n",
"import ai.djl.training.loss.Loss;\n",
"import ai.djl.training.optimizer.Optimizer;\n",
"import ai.djl.translate.Pipeline;\n",
"import ai.djl.training.optimizer.learningrate.LearningRateTracker;\n",
"import ai.djl.translate.Pipeline;\n",
"import ai.djl.training.dataset.ArrayDataset;\n",
"import ai.djl.training.EasyTrain;\n",
"import ai.djl.metric.Metrics;\n",
"import tech.tablesaw.api.*;\n",
"import tech.tablesaw.plotly.api.*;\n",
"import tech.tablesaw.plotly.components.*;\n",
Expand All @@ -373,7 +375,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [],
Expand All @@ -390,42 +392,42 @@
"// we use a stride of 4 to greatly reduce the height and width of the output.\n",
"//Here, the number of output channels is much larger than that in LeNet\n",
" block\n",
" .add(Conv2D.builder()\n",
" .setKernelSize(new Shape(11, 11))\n",
" .add(Conv2d.builder()\n",
" .setKernelShape(new Shape(11, 11))\n",
" .optStride(new Shape(4, 4))\n",
" .setFilters(96).build())\n",
" .add(Activation::relu)\n",
" .add(Pool.maxPool2DBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" .add(Pool.maxPool2dBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" // Make the convolution window smaller, set padding to 2 for consistent\n",
" // height and width across the input and output, and increase the\n",
" // number of output channels\n",
" .add(Conv2D.builder()\n",
" .setKernelSize(new Shape(5, 5))\n",
" .add(Conv2d.builder()\n",
" .setKernelShape(new Shape(5, 5))\n",
" .optPadding(new Shape(2, 2))\n",
" .setFilters(256).build())\n",
" .add(Activation::relu)\n",
" .add(Pool.maxPool2DBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" .add(Pool.maxPool2dBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" // Use three successive convolutional layers and a smaller convolution\n",
" // window. Except for the final convolutional layer, the number of\n",
" // output channels is further increased. Pooling layers are not used to\n",
" // reduce the height and width of input after the first two\n",
" // convolutional layers\n",
" .add(Conv2D.builder()\n",
" .setKernelSize(new Shape(3, 3))\n",
" .add(Conv2d.builder()\n",
" .setKernelShape(new Shape(3, 3))\n",
" .optPadding(new Shape(1, 1))\n",
" .setFilters(384).build())\n",
" .add(Activation::relu)\n",
" .add(Conv2D.builder()\n",
" .setKernelSize(new Shape(3, 3))\n",
" .add(Conv2d.builder()\n",
" .setKernelShape(new Shape(3, 3))\n",
" .optPadding(new Shape(1, 1))\n",
" .setFilters(384).build())\n",
" .add(Activation::relu)\n",
" .add(Conv2D.builder()\n",
" .setKernelSize(new Shape(3, 3))\n",
" .add(Conv2d.builder()\n",
" .setKernelShape(new Shape(3, 3))\n",
" .optPadding(new Shape(1, 1))\n",
" .setFilters(256).build())\n",
" .add(Activation::relu)\n",
" .add(Pool.maxPool2DBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" .add(Pool.maxPool2dBlock(new Shape(3, 3), new Shape(2, 2)))\n",
" // Here, the number of outputs of the fully connected layer is several\n",
" // times larger than that in LeNet. Use the dropout layer to mitigate\n",
" // overfitting\n",
Expand All @@ -450,9 +452,7 @@
" .build())\n",
" // Output layer. Since we are using Fashion-MNIST, the number of\n",
" // classes is 10, instead of 1000 as in the paper\n",
" .add(Linear.builder().setUnits(10).build());\n",
"Model model = Model.newInstance(\"cnn\");\n",
"model.setBlock(block);"
" .add(Linear.builder().setUnits(10).build());"
]
},
{
Expand All @@ -464,55 +464,32 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "2"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Conv2D layer output : (1, 96, 54, 54)\n",
"LambdaBlock layer output : (1, 96, 54, 54)\n",
"LambdaBlock layer output : (1, 96, 26, 26)\n",
"Conv2D layer output : (1, 256, 26, 26)\n",
"LambdaBlock layer output : (1, 256, 26, 26)\n",
"LambdaBlock layer output : (1, 256, 12, 12)\n",
"Conv2D layer output : (1, 384, 12, 12)\n",
"LambdaBlock layer output : (1, 384, 12, 12)\n",
"Conv2D layer output : (1, 384, 12, 12)\n",
"LambdaBlock layer output : (1, 384, 12, 12)\n",
"Conv2D layer output : (1, 256, 12, 12)\n",
"LambdaBlock layer output : (1, 256, 12, 12)\n",
"LambdaBlock layer output : (1, 256, 5, 5)\n",
"LambdaBlock layer output : (1, 6400)\n",
"Linear layer output : (1, 4096)\n",
"LambdaBlock layer output : (1, 4096)\n",
"Dropout layer output : (1, 4096)\n",
"Linear layer output : (1, 4096)\n",
"LambdaBlock layer output : (1, 4096)\n",
"Dropout layer output : (1, 4096)\n",
"Linear layer output : (1, 10)\n"
]
}
],
"outputs": [],
"source": [
"float lr = 0.01f;\n",
"\n",
"Model model = Model.newInstance(\"cnn\");\n",
"model.setBlock(block);\n",
"\n",
"Loss loss = Loss.softmaxCrossEntropyLoss();\n",
"\n",
"LearningRateTracker lrt = LearningRateTracker.fixedLearningRate(lr);\n",
"Optimizer sgd = Optimizer.sgd().setLearningRateTracker(lrt).build();\n",
"\n",
"DefaultTrainingConfig config = new DefaultTrainingConfig(loss).optOptimizer(sgd) // Optimizer (loss function)\n",
" .addEvaluator(new Accuracy()) // Model Accuracy\n",
" .addTrainingListeners(TrainingListener.Defaults.basic()); // Logging\n",
" .addTrainingListeners(TrainingListener.Defaults.logging()); // Logging\n",
"\n",
"Trainer trainer = model.newTrainer(config);\n",
"\n",
"NDArray X = manager.randomUniform(0f, 1.0f, new Shape(1, 1, 224, 224));\n",
"trainer.initialize(X.getShape());\n",
"\n",
Expand Down Expand Up @@ -546,7 +523,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [],
Expand Down Expand Up @@ -600,7 +577,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [],
Expand Down Expand Up @@ -645,16 +622,23 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trainingChapter6(trainIter, testIter, numEpochs, trainer);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Contour Gradient Descent.](https://d2l-java-resources.s3.amazonaws.com/img/chapter_convolution-modern-cnn-AlexNet.png)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down
Loading

0 comments on commit e2b0f8b

Please sign in to comment.