Callbacks in Fastai - callback

I am working on a deep learning project in Fastai and wish to use EarlyStoppingCallback with ReduceLROnPlateauCallback in it. Read callbacks.fastai but struggling to understand how to implement both and couldn't find any relevant example. Any help would be appreciated.
learn = cnn_learner(data, models.resnet50, metrics = [accuracy,quadratic_kappa])
learn.fit(50,2e-6)

The way I normally do it is this way....
First create the learner Object
learn = Learner(data,
model,
loss_func=....,
opt_func=....,
metrics=..... )
learn.unfreeze()
Then you call any callbacks on your fit_one_cycle
learn.fit_one_cycle(16,
max_lr=1e-3,
div_factor=100,
pct_start=0.0,
callbacks = [SaveModelCallback(learn,
name=f'model',monitor='kappa_score')])

maybe you can also do like this-:
from fastai.callbacks import *
from fastai.callback import Callback
from fastai.vision import partial
learner = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=True, callback_fns=[partial(EarlyStoppingCallback, monitor='valid_loss', min_delta=0.01, patience=3)])
# or
learner = cnn_learner(data, models.resnet101, metrics=[accuracy], pretrained=True, callbacks=EarlyStoppingCallback(monitor='valid_loss', min_delta=0.01, patience=3))
learner.fit(150)
# it will stop if "valid_loss" will not decrease (patience=3)

Related

controlling EV3 motor using Matlab

im trying to control my ev3 motor speed, but when I apply this code noting happen what should I do?
thanks
myev3 = legoev3;
mymotor = motor(myev3,'A');
omega_1=[0 17.79123846 18.61264124 19.24807265 19.69118368 19.93754691 19.98470077 19.8321741 19.48149091 18.93615509 18.20161548 17.28521134 16.19609908 14.94516075 13.54489532 12.00929377 10.35369932 8.59465414 6.749734013 4.837372774 2.876678106 0.88724062 -1.11106188 -3.098263017 -5.054507334 -6.960248685 -8.796445531 -10.5447512 -12.1876972 -13.70886776 -15.09306385 -16.32645503 -17.39671767 -18.29315806 -19.00681925 -19.5305706 -19.85917893 -19.98936092 -19.91981582 -19.6512385 -19.18631251 -18.52968322 -17.68791146 -16.66940794 -15.48434921 -14.14457597 -12.66347481 -11.05584439 -9.337747634 -7.526351187 -5.639753927 -3.696806111 -1.716921029 0.280118959 2.274360092 4.245876571 6.174969654 8.042364482 9.829402662 11.5182287 13.0919684 14.53489749 15.83259868
];
%%
q=63;
while q<=62
mymotor.Speed = omega_1(0:q,:);
start(mymotor);
pause(90);
stop(mymotor);
q+1;
end
Your loop runs while q<=62, but you set the value q=63 before that.
So the program never entered the loop to begin with.

What is a correct way to obtain FOnlineSubsystemSteam instance in c++ code

I'm trying to get steam app id and steam user id in my Unreal Engine 4 project in the following way:
if (SteamAPI_Init())
{
IOnlineSubsystem* ossBase = IOnlineSubsystem::Get();
FOnlineSubsystemSteam* oss = Cast<FOnlineSubsystemSteam*>(ossBase);
if (!oss) {
printText(TEXT("Steam Subsystem is down!"), FColor::Red.WithAlpha(255));
return;
}
auto SteamID = FString(std::to_string(SteamUser()->GetSteamID().ConvertToUint64()).c_str());
auto AppID = FString(std::to_string(oss->GetSteamAppId()).c_str());
But it is not possible to convert IOnlineSubsystem to FOnlineSubsystemSteam. So what is a correct way to obtain the instance of FOnlineSubsystemSteam?
The solution is to use static_cast:
FOnlineSubsystemSteam* oss = static_cast<FOnlineSubsystemSteam*>(ossBase);
This one works. It seems obvious to use UE4 Cast, but in this case it does not work.

How do I save a TFDV stats in the correct format for them to be loaded back in?

It is puzzling to me that there is a tfdv.load_statistics() function, but no corresponding tfdv.write_statistics() function. How do I go about saving the statistics, and then loading them again?
e.g.
import tensorflow_data_validation as tfdv
stats = tfdv.generate_statistics_from_dataframe(df)
# how do I save?
# load back for later use
saved_stats = tfdv.load_statistics('saved_stats.stats')
I can save the string representation to a file, but this is not the format that load_statistics expects.
with open('saved_stats.stats', 'w') as o:
o.write(str(stats))
Pointers anyone?
have you tried this : tfdv.utils.stats_util.write_stats_text ?
In the current tfdv version 1.3.0 there are the following methods that can be used:
load_stats_text
write_stats_text
Example:
import tensorflow_data_validation as tfdv
stats = tfdv.generate_statistics_from_dataframe(df)
stats_path = "my-stats-file.stats"
# saving
tfdv.write_stats_text(stats, stats_path)
# loading
stats = tfdv.load_stats_text(stats_path)
Okay figure out this hacky way to do it.
df = ... # create pandas df
from tensorflow_metadata.proto.v0 import statistics_pb2
import tensorflow_data_validation as tfdv
stats = tfdv.generate_statistics_from_dataframe(df)
# save it
with open('saved_stats.stats', 'wb') as o:
o.write(stats.SerializeToString())
# load back for later use
with open('saved_stats.stats', 'rb') as i:
loaded_stats = statistics_pb2.FromString(i.read())
There's a function called tfdv.load_stats_binary that you can use to solve this problem.

How to use jeromq in MATLAB

jeromq is a Java implementation of libzmq. I have a .jar file created from the jeromq source. However, I'm unable to call a class in jeromq from MATLAB. I've used addjavaclasspath and addjavalibrarypath but am still not able to get it working. Does anyone have a simple working example in MATLAB?
I've added the answer here as for reference in case anyone else is interested.
% Author : Dheepak Krishnamurthy
% License : BSD 3 Clause
import org.zeromq.ZMQ;
ctx = zmq.Ctx();
socket = ctx.createSocket(ZMQ.REP);
socket.bind('tcp://127.0.0.1:7575');
message = socket.recv(0);
json_data = native2unicode(message.data)';
message = zmq.Msg(8);
message.put(unicode2native('Received'));
socket.send(message, 0);
socket.close()
My Matlab 9.0.0.341360 (R2016a) wanted the following code instead of import above:
javaclasspath('/path/to/jar/jeromq-0.4.3-SNAPSHOT.jar')
import org.zeromq.*
The rest worked fine.
Here is what I had to do to get things working.
javaclasspath('jeromq-0.5.1.jar')
import org.zeromq.*;
%subscribe to ZMQ feed
context = ZContext();
socket = context.createSocket(ZMQ.SUB);
success = false;
while(~success)
success = socket.connect('tcp://127.0.0.1:5996');
end
socket.subscribe("");
socket.setTCPKeepAlive(1);
%receive a message
message = socket.recv(0); %nonblocking receive uses argument (1)
%when done
socket.close();

How can I add a callback to a ThreadedKernelClient in IPython?

Currently I'm getting my hands on a ThreadedKernelClient by:
kernel_manager = KernelManager(client_class='IPython.kernel.threaded.ThreadedKernelClient')
kernel_manager.start_kernel()
kernel_client = kernel_manager.client()
However, I'm not sure how I can register a callback for incoming messages (after I run execute). Can anyone point me in the right direction?
What I've done is subclass ThreadedZMQSocketChannel and ThreadedKernelClient, and overwrote call_handlers, eg:
class TestChannel(ThreadedZMQSocketChannel):
def call_handlers(self, msg):
# do something
class TestClient(ThreadedKernelClient):
iopub_channel_class = Type(TestChannel)
shell_channel_class = Type(TestChannel)
stdin_channel_class = Type(TestChannel)
Your manager then looks like:
kernel_manager = KernelManager(kernel_name='python', client_class='__main__.TestClient')
Once you start_channels, your call_handlers method will be called with results from methods such as execute.