You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and many other similar variants. It all failed with errors like invalid type: integer `1`, expected a sequence or invalid type: integer `1`, expected an array of length 28.
I even tried to flatten it all
I found a reason — that's because internally it uses NumPy representation. So here my variant of solution:
(I'm using num-traits, but it seemed to be not so so necessary)
#[derive(Deserialize,Debug)]structNpArraySpecs{_py_ver:i64,sym:char,a:Option<i64>,b:Option<i64>,c:Option<i64>,d:i64,e:i64,f:i64,}#[derive(Deserialize,Debug)]structNpArray<T:FromBytes + FromSlicedBytes>{_fmt_id:i64,dimensions:Vec<i64>,specs:NpArraySpecs,b:bool,#[serde(deserialize_with = "from_bytes")]data:Vec<T>,}traitFromSlicedBytes{fnfrom_sliced_le_bytes(data:&[u8]) -> Self;}implFromSlicedBytesforu8{fnfrom_sliced_le_bytes(data:&[u8]) -> Self{
data[0]}}implFromSlicedBytesfori64{fnfrom_sliced_le_bytes(data:&[u8]) -> Self{
i64::from_le_bytes(data.try_into().unwrap())}}fnfrom_bytes<'de,D,T>(deserializer:D) -> Result<Vec<T>,D::Error>whereD:Deserializer<'de>,T:FromBytes + FromSlicedBytes,{let bytes:Value = Deserialize::deserialize(deserializer)?;letValue::Bytes(bytes) = bytes else{returnErr(serde::de::Error::custom("Not a bytes"))};let element_size = size_of::<T>();if bytes.len() % element_size != 0{returnErr(serde::de::Error::custom("Byte array length is not a multiple of element size"));}letmut result = Vec::with_capacity(bytes.len() / element_size);for chunk in bytes.chunks(element_size){
result.push(T::from_sliced_le_bytes(chunk));}Ok(result)}
https://www.kaggle.com/datasets/fedesoriano/qmnist-the-extended-mnist-dataset-120k-images/data
Possibly related to #26
In python it looks like this
So I tried
and
and many other similar variants. It all failed with errors like
invalid type: integer `1`, expected a sequence
orinvalid type: integer `1`, expected an array of length 28
.I even tried to flatten it all
But it failed with
invalid type: sequence, expected u8
The text was updated successfully, but these errors were encountered: