how can I import "Dense3D" and use it in code? - python

I tried to run a code from git but i found a problem with importing Dense3D.
here the part of code witch relate to the problem:
here the part of code that relate to the problem:
import torch
from torch.models import Dense3D
import torch.nn as nn
#some lines deleted
print("Loading options...")
with open(sys.argv[1], 'r') as optionsFile:
options = toml.loads(optionsFile.read())
#Create the model.
model = Dense3D(options)
it make error that "can't import Dense3D" or something like this in another tries with some changes.
and i used colab .

Related

No module named 'augmentations'?

I'm getting this error. It doesn't seem to find the augmentations module. I'm trying to use basicsr for model training.I already tried to install the augmentation module, albumentation, but it doesn't work.
Can someone help me and explain how to solve this?
How it's being imported:
import os.path
import random
import numpy as np
import cv2
import torch
import torch.utils.data as data
import data.util as util
import sys
sys.path.append('../codes/scripts')
sys.path.append('../codes/data')
**import augmentations # Here shows the module import error**
As it is being used below:
# Random Crop (reduce computing cost and adjust images to correct size first)
if img_HR.shape[0] > HR_size or img_HR.shape[1] > HR_size:
#Here the scale should be in respect to the images, not to the training scale (in case they are being scaled on the fly)
scaleor = img_HR.shape[0]//img_LR.shape[0]
img_HR, img_LR = augmentations.random_crop_pairs(img_HR, img_LR, HR_size, scaleor)
Console error:
File "D:\basicsrtrainmodel\BasicSR\codes\data\LRHROTF_dataset.py", line 12, in <module>
import augmentations
ModuleNotFoundError: No module named 'augmentations'
Someone help me please?

How to define a file path when dealing with Tensorflow?

This issue has been solved sorry for any time wasted I'm using notepad++ due to hardware constraints so I wasn't aware of needing to import OS to define a filepath
I am trying to create a TFLite model, I have drawn an arrow to where I'm getting the file path error:
Error: (NameError: name 'oranges' is not defined)
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
assert tf.__version__.startswith('2')
from tensorflow_examples.lite.model_customization.core.data_util.image_dataloader import ImageClassifierDataLoader
from tensorflow_examples.lite.model_customization.core.task import image_classifier
from tensorflow_examples.lite.model_customization.core.task.model_spec import efficientnet_b0_spec
from tensorflow_examples.lite.model_customization.core.task.model_spec import ImageModelSpec
import matplotlib.pyplot as plt
data = ImageClassifierDataLoader.from_folder(oranges) <-- oranges is a folder containing the test
images. It is in the same folder as this file
model = image_classifier.create(data)
loss, accuracy = model.evaluate()
model.export('image_classifier.tflite', 'image_labels.txt')
The path must be a folder containing your images for modelling. In this case, the entry oranges is not defined as a folder path anywhere in the code.
To create a folder path, run:
import os
oranges = os.path.abspath('oranges')
Before executing the code:
data = ImageClassifierDataLoader.from_folder(oranges)

Keras model import name is not defined

I'm not sure why the model isn't defined
Taken from here
https://github.com/DariusAf/MesoNet/blob/master/example.py
Code:
from classifiers import *
from pipeline import *
from keras.preprocessing.image import ImageDataGenerator
classifier = Meso4()
classifier.load('Meso4_DF')
gives error:
classifier = Meso4()
NameError: name 'Meso4' is not defined
The reason for this is that Meso4 is defined in classifiers.py, as you can see here.
Strictly speaking, your problem would be solved by also downloading the classifiers.py file and putting it in the same directory as your example.py file.
However, you should, in general, refrain from copy-pasting code from GitHub unless you know what you are doing, and if you need to wonder if you do, you don't.
Therefore, I recommend actually cloning the repo and working from the local copy.

pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest

I have built a custom sklearn pipeline, as follows:
pipeline = make_pipeline(
SelectColumnsTransfomer(features_to_use),
ToDummiesTransformer('feature_0', prefix='feat_0', drop_first=True, dtype=bool), # Dummify customer_type
ToDummiesTransformer('feature_1', prefix='feat_1'), # Dummify the feature
ToDummiesTransformer('feature_2', prefix='feat_2'), # Dummify
ToDummiesTransformer('feature_3', prefix='feat_3'), # Dummify
)
pipeline.fit(df)
The classes SelectColumnsTransfomer and ToDummiesTransformer are custom sklearn steps implementing BaseEstimator and TransformerMixin.
To serialise this object I use
from sklearn.externals import joblib
joblib.dump(pipeline, 'data_pipeline.joblib')
but when I do deserialise with
pipeline = joblib.load('data_pipeline.joblib')
I get AttributeError: module '__main__' has no attribute 'SelectColumnsTransfomer'.
I have read other similar questions and followed the instruction in this blogpost here, but couldn't solve the issue.
I am copying pasting the classes, and importing them in the code. If i create a simplified version of this exercise, the whole thing works, the problem occurs because i am running some tests with pytest, and when i run pytest it seems it doesn't see my custom classes, in fact there is this other part of the error
self = <sklearn.externals.joblib.numpy_pickle.NumpyUnpickler object at 0x7f821508a588>, module = '__main__', name = 'SelectColumnsTransfomer' which is hinting me that the NumpyUnpickler doesn't see the SelectColumnsTransfomer even if in the test it is imported.
My test code
import pytest
from app.pipeline import * # the pipeline objects
# SelectColumnsTransfomer and ToDummiesTransformer
# are here!
#pytest.fixture(scope="module")
def clf():
pipeline = joblib.load("persistence/data_pipeline.joblib")
return clf
def test_fake(clf):
assert True
I had the same error message when I was trying to save a Pytorch class like this:
import torch.nn as nn
class custom(nn.Module):
def __init__(self):
super(custom, self).__init__()
print("Class loaded")
model = custom()
And then using Joblib to dump this model like so:
from joblib import dump
dump(model, 'some_filepath.jobjib')
The issue was I was running the code above in a Kaggle kernel. And then downloading the dumped file and trying to load it with this script locally:
from joblib import load
model = load(model, 'some_filepath.jobjib')
The way I fixed the issue was to run all of these code snippets locally on my computer instead of creating the class and dumping it on Kaggle, but loading it on my local machine. Wanted to add this here because the comments on the answer by #DarioB confused me in their reference to a 'function' which didn't apply in my simpler case.
I had a similar issue with sklearn and complex pipelines.
I used cloudpickle 2.0.0 /py3.10 (instead of pickle or joblib) to dump the model and then load it with joblib without error.
Hope it could help.
Note: the model was dump from a jupyter notebook and load inside a python script.

Python scripting with OVITO

I have a quick question regarding my python code. I am using a tool called OVITO, which works quite nicely when I run in the GUI. I am trying to run it using the python scripting interface, but I am not wanting to get the results that I want.
The code should create a list of files with a single number. However, the number for each file should change. I am calculating a specific property for my material (vacancies), and I know vacancies are created, but my code says 0 for all my files. Can someone tell me if I am overwriting somehow or if something is obviously wrong? Again, I do not expect anyone here to know OVITO, but just with the python part, curious if something is clearly wrong.
Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
# Import NumPy module.
import numpy
import sys
node = import_file("../cascade.dump",multiple_frames = True)
for i in range(node.source.num_frames):
with open("{}.out".format(i),'w') as f:
mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
mod.reference.load("../../../../../../STP/position_perfect_300.dump")
node.modifiers.append(mod)
node.compute()
node.modifiers.append(SelectExpressionModifier(expression = 'Occupancy.1==0&&Occupancy.2==0 && ParticleType==1'))
node.compute()
f.write("%i\n" % numpy.count_nonzero(node.output.particle_properties['Selection']))
f.close()
It's a very old post, but to whom it can help the solution is just to give the frame to compute in node.compute().
Import OVITO modules.
from ovito.io import *
from ovito.modifiers import *
# Import NumPy module.
import numpy
import sys
node = import_file("../cascade.dump",multiple_frames = True)
for i in range(node.source.num_frames):
with open("{}.out".format(i),'w') as f:
mod = WignerSeitzAnalysisModifier(per_type_occupancies = True)
mod.reference.load("../../../../../../STP/position_perfect_300.dump")
node.modifiers.append(mod)
node.compute(i)
node.modifiers.append(SelectExpressionModifier(expression = 'Occupancy.1==0&&Occupancy.2==0 && ParticleType==1'))
node.compute(i)
f.write("%i\n" % numpy.count_nonzero(node.output.particle_properties['Selection']))
f.close()

Categories