Skip to content

[BUG Report]: load_weights fails to open HDF5 file #1251

Open
@Sahasrara

Description

@Sahasrara

Description

Here's the exception I'm getting:

DllNotFoundException: hdf5 assembly:<unknown assembly> type:<unknown type> member:(null)
HDF.PInvoke.H5F..cctor () (at /home/appveyor/projects/hdf-pinvoke-1-10/submodules/HDF.PInvoke/HDF5/H5Fpublic.cs:41)
Rethrow as TypeInitializationException: The type initializer for 'HDF.PInvoke.H5F' threw an exception.
HDF5CSharp.Hdf5.OpenFile (System.String filename, System.Boolean readOnly, System.Boolean attemptShortPath) (at <a573135056b64eceaab6f7dd4003494c>:0)
Tensorflow.Keras.Engine.Model.load_weights (System.String filepath, System.Boolean by_name, System.Boolean skip_mismatch, System.Object options) (at <3504e8007fee496baf2c8fdd9578867a>:0)
Slay.Crepe.BuildAndLoadModel (Slay.Crepe+ModelCapacity capacity) (at Assets/Scripts/Sound/AI/Crepe.cs:78)
Slay.Crepe.Start () (at Assets/Scripts/Sound/AI/Crepe.cs:13)

Reproduction Steps

This is the entirety of my code. The "Start()" method is called when the application starts.

using System.Collections.Generic;
using Tensorflow;
using Tensorflow.Keras.Engine;
using UnityEngine;
using static Tensorflow.KerasApi;

namespace Slay
{
    public class Crepe : MonoBehaviour
    {
        private void Start()
        {
            Debug.Log(BuildAndLoadModel(k_CapacityFull));
        }

        #region Constants
        private const int k_ModelSampleRate = 16000;
        private static readonly long[] k_Layers = new long[] { 1, 2, 3, 4, 5, 6 };
        private static readonly long[] k_Widths = new long[] { 512, 64, 64, 64, 64, 64 };
        private static readonly long[][] k_Strides = new long[][]
        {
            new long[] { 4, 1 },
            new long[] { 1, 1 },
            new long[] { 1, 1 },
            new long[] { 1, 1 },
            new long[] { 1, 1 },
            new long[] { 1, 1 },
        };
        private static readonly ModelCapacity k_CapacityTiny = new ModelCapacity("tiny", 4);
        private static readonly ModelCapacity k_CapacitySmall = new ModelCapacity("small", 8);
        private static readonly ModelCapacity k_CapacityMedium = new ModelCapacity("medium", 16);
        private static readonly ModelCapacity k_CapacityLarge = new ModelCapacity("large", 24);
        private static readonly ModelCapacity k_CapacityFull = new ModelCapacity("full", 32);
        #endregion

        #region State
        private Dictionary<ModelCapacity, IModel> m_LoadedModels;
        #endregion

        #region Core
        private IModel BuildAndLoadModel(ModelCapacity capacity)
        {
            if (m_LoadedModels == null)
                m_LoadedModels = new();
            IModel model;
            if (m_LoadedModels.TryGetValue(capacity, out model))
                return model;

            Tensors x = keras.layers.Input(shape: 1024, name: "input", dtype: TF_DataType.TF_FLOAT);
            Tensors y = keras.layers.Reshape(target_shape: (1024, 1, 1)).Apply(x);
            for (int i = 0; i < k_Layers.Length; i++)
            {
                long layer = k_Layers[i];
                int filter = capacity.Filters[i];
                long width = k_Widths[i];
                long[] stride = k_Strides[i];
                y = keras
                    .layers.Conv2D(
                        filter,
                        (width, 1),
                        strides: stride,
                        padding: "same",
                        activation: "relu"
                    )
                    .Apply(y);
                y = keras.layers.BatchNormalization(name: $"conv{layer}-BN").Apply(y);
                y = keras
                    .layers.MaxPooling2D(pool_size: (2, 1), strides: null, padding: "valid")
                    .Apply(y);
                y = keras.layers.Dropout(0.25f).Apply(y);
            }
            y = keras.layers.Permute(new int[] { 2, 1, 3 }).Apply(y);
            y = keras.layers.Flatten().Apply(y);
            y = keras.layers.Dense(360, activation: "sigmoid").Apply(y);

            model = keras.Model(inputs: x, outputs: y);
            string path = $"{Application.streamingAssetsPath}/models/model-{capacity.Name}.h5";
            model.load_weights(path);
            // model.compile(keras.optimizers.Adam(), keras.losses.BinaryCrossentropy());

            return model;
        }
        #endregion

        #region Helpers
        private class ModelCapacity
        {
            private string m_Name;
            private int m_Capacity;
            private int[] m_Filters;

            public ModelCapacity(string name, int capacity)
            {
                m_Name = name;
                m_Capacity = capacity;
                m_Filters = new int[]
                {
                    32 * capacity,
                    4 * capacity,
                    4 * capacity,
                    4 * capacity,
                    8 * capacity,
                    16 * capacity,
                };
            }

            public string Name => m_Name;
            public int Capacity => m_Capacity;
            public int[] Filters => m_Filters;
        }
        #endregion
    }
}

Known Workarounds

N/A

Configuration and Other Information

I'm running this on Unity 2023.2.13f1 on Windows 11.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions