Package: keras4torch
A compatible-with-keras wrapper for training PyTorch models✨Documentations•Dev Logs•Mini Versionkeras4torchprovides a high-level API to train PyTorch models compatible with Keras. This project is designed for beginner with these objectives:Help people who are new to PyTorch but familar with KerasReduce the cost for migrating Keras model implementation to PyTorchUsekeras4torchfor Kaggle's code competition! Check thispackage datasetandstarter notebook.Installationpip install keras4torchPyTorch 1.6+ and Python 3.6+ is required.Quick startSuppose you have ann.Moduleto train.model=torchvision.models.resnet18(num_classes=10)All you need to do is wrapping it viak4t.Model().importkeras4torchask4tmodel=k4t.Model(model)Now, there're two workflows can be used for training.TheNumPy workflowis compatible with Keras..compile(optimizer, loss, metrics)for settings of optimizer, loss and metrics.fit(x, y, epochs, batch_size, ...)takes raw numpy input for training.evaluate(x, y)outputs adictresult of your metrics.predict(x)for doing predictionsAndDataLoader workflowis more flexible and of pytorch style..compile(optimizer, loss, metrics)same as NumPy workflow.fit_dl(train_loader, val_loader, epochs)for training the model viaDataLoader.evaluate_dl(data_loader)same as NumPy workflow but takesDataLoader.predict_dl(data_loader)same as NumPy workflow but takesDataLoaderThe two workflows can be mixed.MNIST exampleHere we show a complete example of training a ConvNet on MNIST.importtorchimporttorchvisionfromtorchimportnnimportkeras4torchask4tStep1: Preprocess datamnist=torchvision.datasets.MNIST(root='./',download=True)x,y=mnist.train_data.unsqueeze(1),mnist.train_labelsx=x.float()/255.0# scale the pixels to [0, 1]x_train,y_train=x[:40000],y[:40000]x_test,y_test=x[40000:],y[40000:]Step2: Define the modelIf you have ann.Modulealready, just wrap it viak4t.Model. For example,model=torchvision.models.resnet50(num_classes=10)model=k4t.Model(model)For building models from scratch, you can useKerasLayer(located ink4t.layers) for automatic shape inference, which can free you from calculating the input channels.As is shown below,k4t.layers.Conv2d(32, kernel_size=3)equalsnn.Conv2d(?, 32, kernel_size=3)where the first parameter?(i.e.in_channels) will be determined by itself.model=torch.nn.Sequential(k4t.layers.Conv2d(32,kernel_size=3),nn.ReLU(),nn.MaxPool2d(2,2),k4t.layers.Conv2d(64,kernel_size=3),nn.ReLU(),nn.Flatten(),k4t.layers.Linear(10))A model containingKerasLayerneeds an extra.build(input_shape)operation.model=k4t.Model(model).build([1,28,28])Step3: Summary the modelmodel.summary()=========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
=========================================================================================
├─Conv2d*: 1-1                           [-1, 32, 26, 26]          320
├─ReLU: 1-2                              [-1, 32, 26, 26]          --
├─MaxPool2d: 1-3                         [-1, 32, 13, 13]          --
├─Conv2d*: 1-4                           [-1, 64, 11, 11]          18,496
├─ReLU: 1-5                              [-1, 64, 11, 11]          --
├─Flatten: 1-6                           [-1, 7744]                --
├─Linear*: 1-7                           [-1, 10]                  77,450
=========================================================================================
Total params: 96,266
Trainable params: 96,266
Non-trainable params: 0
Total mult-adds (M): 2.50
=========================================================================================Step4: Config optimizer, loss and metricsmodel.compile(optimizer='adam',loss=nn.CrossEntropyLoss(),metrics=['acc'])If GPU is available, it will be used automatically. You can also passdeviceparameter to.compile()explicitly.Step5: Traininghistory=model.fit(x_train,y_train,epochs=30,batch_size=512,validation_split=0.2,)Train on 32000 samples, validate on 8000 samples:
Epoch 1/30 - 2.8s - loss: 0.6109 - acc: 0.8372 - val_loss: 0.2712 - val_acc: 0.9235 - lr: 1e-03
Epoch 2/30 - 1.5s - loss: 0.2061 - acc: 0.9402 - val_loss: 0.1494 - val_acc: 0.9579 - lr: 1e-03
Epoch 3/30 - 1.5s - loss: 0.1202 - acc: 0.9653 - val_loss: 0.0974 - val_acc: 0.9719 - lr: 1e-03
Epoch 4/30 - 1.5s - loss: 0.0835 - acc: 0.9757 - val_loss: 0.0816 - val_acc: 0.9769 - lr: 1e-03
... ...Step6: Plot learning curvehistory.plot(kind='line', y=['loss', 'val_loss'])Step7: Evaluate on test setmodel.evaluate(x_test,y_test){'loss': 0.06655170023441315, 'acc': 0.9839999675750732}CommunicationWe have activatedGithub Discussionfor Q&A and most general topics!For bugs report, please useGithub Issues.