Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelSaver fails on saving Input with 2d and 3d tensors #160

Closed
zaleslaw opened this issue Jul 27, 2021 · 2 comments · Fixed by #177
Closed

ModelSaver fails on saving Input with 2d and 3d tensors #160

zaleslaw opened this issue Jul 27, 2021 · 2 comments · Fixed by #177
Assignees
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@zaleslaw
Copy link
Collaborator

zaleslaw commented Jul 27, 2021

During saving model, trained on IrisData

const val SEED = 12L
const val TEST_BATCH_SIZE = 5
const val EPOCHS = 100
const val TRAINING_BATCH_SIZE = 3
const val PATH_TO_MODEL = "models/irisNet"

val model = Sequential.of(
    Input(4),
    Dense(5, Activations.Relu, kernelInitializer = HeNormal(SEED), biasInitializer = HeUniform(SEED)),
    Dense(3, Activations.Linear, kernelInitializer = HeNormal(SEED), biasInitializer = HeUniform(SEED))
)

fun main() {
    data.shuffle()

    val dataset = OnHeapDataset.create(
        ::extractX,
        ::extractY
    )

    val (train, test) = dataset.split(0.8)

    model.use {
        it.compile(optimizer = Adam(), loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS, metric = Metrics.ACCURACY)

        it.summary()
        it.fit(dataset = train, epochs = EPOCHS, batchSize = TRAINING_BATCH_SIZE)

        val accuracy = model.evaluate(dataset = test, batchSize = TEST_BATCH_SIZE).metrics[Metrics.ACCURACY]

        model.save(modelDirectory = File(PATH_TO_MODEL), savingFormat = SavingFormat.JSON_CONFIG_CUSTOM_VARIABLES, writingMode = WritingMode.OVERRIDE)

        println("Accuracy: $accuracy")
    }
}

it fails with the

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:459)
	at org.jetbrains.kotlinx.dl.api.inference.keras.ModelSaverKt.saveModelConfiguration(ModelSaver.kt:54)
	at org.jetbrains.kotlinx.dl.api.inference.keras.ModelSaverKt.saveModelConfiguration$default(ModelSaver.kt:36)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.saveModel(GraphTrainableModel.kt:794)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.saveInKerasFormat(GraphTrainableModel.kt:788)
	at org.jetbrains.kotlinx.dl.api.core.GraphTrainableModel.save(GraphTrainableModel.kt:783)
	at org.jetbrains.kotlinx.dl.api.core.TrainableModel.save$default(TrainableModel.kt:259)
	at IrisKt.main(Iris.kt:45)
	at IrisKt.main(Iris.kt)

due to unfixed TODO in ModelSaver

  1. Please, fix the bug to handle not only images, but all situations to save the Input layer correctly to JSON config
  2. It should work in both ways: for reading and for writing Input layer
  3. Don't forget to add some tests for 1d and 3d data
@zaleslaw zaleslaw added bug Something isn't working good first issue Good for newcomers labels Jul 27, 2021
@zaleslaw zaleslaw modified the milestones: 0.2, 0.3 Jul 27, 2021
@juliabeliaeva
Copy link
Contributor

Hi, I'd like to work on this issue.

@zaleslaw
Copy link
Collaborator Author

zaleslaw commented Aug 6, 2021

Great, @juliabeliaeva feel free to ask any question in this issue or on Kotlin slack

juliabeliaeva added a commit to juliabeliaeva/KotlinDL that referenced this issue Aug 7, 2021
juliabeliaeva added a commit to juliabeliaeva/KotlinDL that referenced this issue Aug 7, 2021
This code only worked for a specific number of dimensions, while being unnecessary since createKerasInputLayer method already sets batch_input_shape field correctly.

Kotlin#160
juliabeliaeva added a commit to juliabeliaeva/KotlinDL that referenced this issue Aug 7, 2021
zaleslaw pushed a commit that referenced this issue Aug 11, 2021
* Extract createInputLayer function

* Simplify loading model layers

* Allow loading models with any input layer dimensions

#160

* Simplify GraphTrainableModel#serializeModel

* Remove unnecessary code setting batch_input_shape

This code only worked for a specific number of dimensions, while being unnecessary since createKerasInputLayer method already sets batch_input_shape field correctly.

#160

* Test that input layer dimensions are saved and loaded correctly

#160
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants